1.selenium+java+maven+testNg 的安装

1.搭建环境之前,要先安装java的jdk、idea工具,请自行安装

①Java运行环境–jdk

https://www.oracle.com/java/technologies/javase-jdk11-downloads.html

②IDE–idea
https://www.jetbrains.com/idea/download/#section=mac

③下载chrom对应的chromdriver,先看自己的浏览器的版本,再去下载对应的chromdriver,不然程序会报错

https://chromedriver.storage.googleapis.com/index.html

下载后得到的文件,在访达中打开,把下载好的文件拖动到 /usr/local/bin目录下

打开/usr/local/bin的方法
    1 打开mac自带命令行,输入:cd /usr/local/bin
    2 再输入:open .则成功打开此目录

 

这时chromdriver就配置好了,可以打开命令行输入chromedriver --version来看,能返回具体的版本号则表示安装成功~

2.在idea里面新建一个maven项目,然后在pom.xml里面需要引入几个依赖包。

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>3.141.59</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>3.141.59</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.141.59</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.3.0</version>
        <scope>test</scope>
    </dependency>

3. 运行一个例子

System.setProperty("webdriver.chrome.driver","C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");//这里输入chromedriver的安装目录   windows
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;


public class Test {
    public static void main(String[] args) {
      
        System.setProperty("webdriver.chrome.driver","/usr/local/bin/chromedriver");//这里输入chromedriver的安装目录
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com/");
        driver.findElement(By.name("wd")).click();
        driver.findElement(By.name("wd")).sendKeys("selenium教程" );
        driver.findElement(By.id("su")).click();
        try {
            Thread.sleep(2000);
        }catch(InterruptedException e){
            System.out.print("线程异常");
        }
        
        driver.quit();
    }
}

 

posted @ 2020-08-24 17:04  裴敏儿  阅读(270)  评论(0编辑  收藏  举报