学习Selenium遇到的问题和解决方案

问题1:IE驱动位数问题,未安装对应的IE,打不开IE浏览器(已解决20180323)

 使用Selenium启动IE浏览器的时候,报错,报错信息如下

 org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. IELaunchURL() returned HRESULT 80070002 ('系统找不到指定的文件。') for URL 'http://localhost:36608/' (WARNING: The server did not provide any     stacktrace information)

单纯看这个报错信息,看不太明白,在网上搜寻答案无果,但是看到了增加打印日志的语句

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability("logLevel", "DEBUG");
driver = new InternetExplorerDriver(capabilities);

问题原因:通过查看DEBUG级别的日志,可以看到错误原因是因为找不到文件所以打不开IE,于是联想到我的IE驱动是32位的,而我只安装了64位的IE,没有装32位的IE浏览器,所以才会打不开。

解决方案:更换为64位的IE驱动,完美解决此问题。

附上驱动下载地址:下载IEDriver

问题2:打开https地址时,IE浏览器弹出提示窗口,导致程序无法继续运行(已解决20180323)

selenium在打开地址"https://www.baidu.com/"时报错

org.openqa.selenium.UnhandledAlertException: Modal dialog present: 是否只查看安全传送的网页内容?

问题原因:https安全协议导致ie浏览器弹窗(Modal dialog),阻止了程序运行

解决方案:1.将https改为http后不再弹出对话窗可以正常运行,但没从根本解决问题

                   2.可以在弹出窗口之后插入一句代码driver.switchTo().alert().accept(); 对弹出对话框进行处理,程序可以继续运行

问题3:selenium打开chrome时,chromedriver.exe停止运行(已解决20180323)

程序在打开chrome浏览器时报错

org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.

问题原因:chromedriver版本和chrome浏览器版本不兼容,详见https://blog.csdn.net/huilan_same/article/details/51896672,列出了兼容的chromedriver和对应的chrome浏览器版本

解决方案:因为我的chrome浏览器是版本是56,更换chromedriver版本为2.28,解决问题

原文地址:https://www.cnblogs.com/fpzh/p/8628944.html