介绍如何启动chrome浏览器
public static void OpenChrome() {
//设置chrome浏览器的安装位置
System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
//设置chromedriver的位置
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
// 初始化一个chrome浏览器实例,实例名称叫driver
WebDriver driver = new ChromeDriver();
// get()打开一个站点
driver.get("https://www.baidu.com");
// getTitle()获取当前页面title的值
System.out.println("当前打开页面的标题是: " + driver.getTitle());
// 关闭并退出浏览器
driver.quit();
}
运行,会弹出chrome浏览器的界面,然后马上变成百度首页,并在控制台如程序要求有以下输出:
问题解决:
(1).虽然chrome正常启动了,但是并没有执行get百度首页的操作,控制台当然也没有打印信息,报错信息如下:
[12140:12000:0605/224013.947:ERROR:cache_util_win.cc(20)] Unable to move the cache: 5 [12140:12000:0605/224013.947:ERROR:cache_util.cc(134)] Unable to move cache folder C:\Users\Yoga\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache to C:\Users\Yoga\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GPUCache_000 [12140:12000:0605/224013.947:ERROR:disk_cache.cc(132)] Unable to create cache [12140:12000:0605/224013.947:ERROR:shader_disk_cache.cc(593)] Shader Cache Creation failed: -2 <span>Exception in thread "main" org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start. Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'</span> System info: host: 'LENOVO-PC', ip: '192.168.56.1', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_51' Driver info: driver.version: ChromeDriver at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:193) at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:181) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236) at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:184) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:171) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:124) at selenium.WebDriverTest.Demo1.main(Demo1.java:17) Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:25606/status] to be available after 20012 ms at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:107) at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:190) ... 10 more Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:140) at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:80) ... 11 more Caused by: java.util.concurrent.TimeoutException at java.util.concurrent.FutureTask.get(Unknown Source) at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:128) ... 12 more
从报错可以看出chromedriver无法调用,电脑上装有chrome浏览器,所以一开始以为在配置测试的浏览器时System.setProperty()中填入的就是chrome的安装位置,即最开始代码中是这样的:System.setProperty("webdriver.chrome.driver", "C://Program Files (x86)//Google//Chrome//Application//chrome.exe");经查阅得知selenium操作chrome浏览器是通过chromedriver这样一个东西,所以还必须下载对应的浏览器driver,然后配置时使用这个就行了,即代码改为:System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");。下载chromedriver的网址在:https://chromedriver.storage.googleapis.com/index.html
(2).当我将System.setProperty中的chrome.exe换成chromedriver.exe以后,运行程序,同样弹出了chrome浏览器,但依然不能访问百度,浏览器上出现了类似“xxx不安全xxx”字样,控制台有以下错误:
<span>unknown error: Runtime.executionContextCreated has invalid ‘context’: </span>{“auxData”:{“frameId”:”11740.1”,”isDefault”:true},”id”:1,”name”:”“,”origin”:”://”} (Session info: chrome=xx.xx (Driver info: chromedriver=2.9.248307,platform=Windows xx)
原因是chromedriver和chrome版本不匹配,老版本的chromedriver无法正常启动chrome,解决办法还是在这个网址上下载最新的chromedriver:https://chromedriver.storage.googleapis.com/index.html,
在notes.txt中可以详细说明了各个版本的chromedriver和chrome浏览器的配套关系。
(3)selenium-server-standalone的版本号一定要跟selenium-java的版本号一致,否则运行时会报错。
selenium的下载地址:http://selenium-release.storage.googleapis.com/index.html