介绍如何启动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

Java+Selenium3.0----------启动谷歌浏览器及错误处理相关推荐

  1. elasticsearch5.0启动出现的错误(自己做过测试,有错误可以在这上面找)

    elasticsearch5.0启动失败,出现如下提示: 1.Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x ...

  2. elasticsearch5.0启动出现的错误

    elasticsearch5.0启动失败,出现如下提示: 1.Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x ...

  3. Java Selenium3 WebDriver启动火狐、Chrome、IE,Edge浏览器的方法(一)

    selenium3和selenium2没有太大的区别,就是精简了一些不用的东西,对浏览器的支持更好了,比如对高版本的浏览器(FireFox,Chrome,Edge等)可以完美的支持,不用受限于版本的问 ...

  4. Java + Selenium3.0 + Maven+Excel + Log4j2 的关键字驱动框架(Design-StepbyStep)

    关键子驱动的框架:关键字驱动框架- 它更多是作为一个开发或者测试开发人员,开发的一个框架或者工具,目的是让不懂代码的黑盒测试人员去快速编写测试脚本. 一:关键字驱动框架介绍 二:搭建关键字驱动框架的步 ...

  5. Java+Selenium3.3.1环境搭建

    一.背景和目的 selenium从2.0开始,加入了webdriver,实际上,我们说的selenium自动化测试,大部分情况都是在使用webdriver的API.现在去Selenium官网,发现最新 ...

  6. 【Java从0到架构师】Dubbo 基础 - 设置启动时检查、直接提供者、线程模型、负载均衡、集群容错、服务降级

    Dubbo 分布式 RPC 分布式核心基础 分布式概述 RPC Dubbo Dubbo 入门程序 - XML.注解 部署管理控制台 Dubbo Admin 修改绑定的注册 IP 地址 设置启动时检查 ...

  7. Spring Tool Suite(STS)启动时出现错误:Java was started but returned exit code=13问题解决

    Spring Tool Suite(STS)启动时出现错误:Java was started but returned exit code=13问题解决 参考文章: (1)Spring Tool Su ...

  8. 启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099的解决办法

    启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099的解决办法 参考文章: (1)启动to ...

  9. java d 配置不生效_Tomcat启动失败问题 (指定的主资源集 [D:\java\apache-tomcat-9.0.35\webapps\AppManageSystem] 无效)...

    警告: 部署描述符[D:\java\apache-tomcat-9.0.35\conf\Catalina\localhost\AppManageSystem.xml]中值为[/AppManageSys ...

最新文章

  1. ISO9000机房管理办法
  2. 【矩阵运算c++实现】矩阵封装实现Matrix类
  3. Java泛型深入题目
  4. 快速理解Token,Cookie,Session
  5. leetcode算法题--掷骰子模拟★★
  6. [云炬python3玩转机器学习]sklearn中的Scaler
  7. php不支持定时器么,PHP没有定时器?
  8. 在load事件中关闭窗体
  9. 安卓4.4不支持touchend事件解决办法
  10. python3.7.2安装与pycharm_Python3和PyCharm安装与环境配置【图文教程】
  11. 好的技术不一定能给你带来财富,但是好的工具一定可以让你创造财富
  12. 2021-06-17 compareAndSet 比较交换 CAS
  13. linux —— ubuntu 初次安装问题
  14. SSL之父称SSL不会因被攻击而失去生命力
  15. Matlab 输入输出命令
  16. DM420步进电机驱动器
  17. nginx 解析二级域名
  18. npm方法创建一个vue项目,引入element插件
  19. 文本转思维导图(xmind)
  20. PYGAME - Event 事件

热门文章

  1. JAVA并发包内容_java并发包
  2. Oracle 19c RAC打补丁过程避坑指南
  3. MySQL 8.0.12 有什么新内容?
  4. 杨廷琨Oracle Code大会分享:如何编写高效SQL(含PPT)
  5. 常用的echo和cat,这次让我折在了特殊字符丢失问题上
  6. 教你实现一个朴实的Canvas时钟效果
  7. 用“黑科技”产放心粮,种地竟然和想象中有点不一样
  8. 和当前时间比较_货币的时间价值
  9. 计算机图形学E11——B样条曲线
  10. Python3常见内嵌函数