selenium使用chrome浏览器

之前的自动化测试脚本一直使用的是firefox浏览器,因为简单方便,不需要额外去安装driver驱动,而且我对firefoxfirebug插件也情有独钟,定位元素时使用起来相当方便,但是后期有的项目对chrome浏览器的兼容性最好,使用firefox浏览器会出现元素定位不准确的现象,所以还是需要使用到chrome浏览器进行自动化测试。

1.下载与chrome版本相匹配的chromedriver版本,以下贴出部分版本较新的chrome和chromedriver的版本映射表,我使用的chrome版本为v58,所以下载v2.29的chromedriver版本。下载地址就不贴了,CSDN上资源很丰富。

chromedriver版本

支持的Chrome版本

v2.29

v56-58

v2.28

v55-57

v2.27

v54-56

v2.26

v53-55

v2.25

v53-55

v2.24

v52-54

v2.23

v51-53

v2.22

v49-52

v2.21

v46-50

v2.20

v43-48

v2.19

v43-47

v2.18

v43-46

v2.17

v42-43

v2.13

v42-45

v2.15

v40-43

2.修改脚本

使用firefox时,测试脚本为:

 

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class test {
 
WebElement webElement = null;
WebDriver driver = new FirefoxDriver();
 
@BeforeTest
public void open() {
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
}
 
@Test(priority = 1)
public void searchCSDN() throws InterruptedException {
webElement = driver.findElement(By.xpath("//input[@id = \"kw\"]"));
webElement.sendKeys("csdn");
webElement = driver.findElement(By.xpath("//*[@id=\"su\"]"));
webElement.click();
Thread.sleep(3000);
}
}

首先,我将下载的chromedriver.zip文件解压到F:/test文件夹下

 

selenium使用chrome,网上主要有两种方案:

第一种,将chromedriver.exe路径F:/test/chromedriver.exe添加到path环境变量中,但是运行脚本,报错---失败

第二种,通过webdriver.chrome.driver系统属性实现,修改脚本如下:

 

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class test {
 
WebElement webElement = null;
WebDriver driver = new ChromeDriver();
 
@BeforeTest
public void open() {
// 告诉系统chromedriver.exe的位置
System.setProperty("webdriver.chrome.driver","F:\\test\\chromedriver.exe");
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
}
 
@Test(priority = 1)
public void searchCSDN() throws InterruptedException {
webElement = driver.findElement(By.xpath("//input[@id = \"kw\"]"));
webElement.sendKeys("csdn");
webElement = driver.findElement(By.xpath("//*[@id=\"su\"]"));
webElement.click();
Thread.sleep(3000);
}
}

运行脚本,仍然报错,提示Cannot instantiate class helloworld.test

 

 

百思不得其解啊,后面琢磨了好久,发现原因是下载下来的chromedriver.exe并不是随便放在哪个路径都可以,而是要放在chrome的安装目录下,如将chromedriver.exe放在D:\Program Files (x86)\Google\Chrome\Application下,这个时候,脚本中即使不添加

 

System.setProperty("webdriver.chrome.driver","D:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");

都可以成功运行脚本。

 

So,最终正确运行的脚本如下:

 

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class test {
 
WebElement webElement = null;
WebDriver driver = new ChromeDriver();
 
@BeforeTest
public void open() {
// 告诉系统chromedriver.exe的位置,可加可不加
System.setProperty("webdriver.chrome.driver","D:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
}
 
@Test(priority = 1)
public void searchCSDN() throws InterruptedException {
webElement = driver.findElement(By.xpath("//input[@id = \"kw\"]"));
webElement.sendKeys("csdn");
webElement = driver.findElement(By.xpath("//*[@id=\"su\"]"));
webElement.click();
Thread.sleep(3000);
}
}

 

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值