http://www.open-open.com/lib/view/open1354764154148.html
1. Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remoteserver or browser start-up failure.
1、下载一个系统兼容的IEDriver,可以从这里下载(http://code.google.com/p/selenium/downloads/list),我下载了IEDriverServer_Win32_2.31.0.zip
2、解压,放在目录下,我放在了IE安装目录下(C:\Program Files\Internet Explorer)
3、修改代码,System.setProperty("webdriver.ie.driver","C:\\Program Files\\Internet Explorer\\IEDriverServer.exe");
添加IE驱动:
System.setProperty("webdriver.ie.driver","C:\\Program Files\\Internet Explorer\\IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
webdriver = new InternetExplorerDriver(ieCapabilities);
==================================================================================================================
2.使用ChromeDriver时要使用webdriver.chrome.driver来指定chromeDriver的位置,具体见实例:<单独下载chromedriver.exe,如问题1:(http://code.google.com/p/selenium/downloads/list)>
package selenium.test.googleSearch;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BaiduChromeDriver {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//设置访问ChromeDriver的路径
System.setProperty("webdriver.chrome.driver", "C:\\Documents and Settings\\Administrator\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com/");
}
}
ps:
chrome浏览器安装时默认路径如下:
OS | Expected Location of Chrome |
Linux | /usr/bin/google-chrome1 |
Mac | /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome |
Windows XP | %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe |
Windows Vista | C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe |
chrome不能够手动选择安装路径,只能在上述对应的目录下查看chrome文件信息
******************Chrome****************************************************************************************
Started ChromeDriver
port=17034
version=22.0.1203.0b
log=E:\LICMWork\testmvn\chromedriver.log
[0730/173528:ERROR:automation_proxy.cc(320)] Channel error in AutomationProxy.
Exception in thread "main" org.openqa.selenium.WebDriverException: Chrome did not respond to 'GetChromeDriverAutomationVersion'. Elapsed time was 1 ms. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 12.91 seconds
Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39'
System info: host: 'licm', ip: '172.19.41.30', os.name: 'Windows Vista', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_10-rc2'
Driver info: org.openqa.selenium.chrome.ChromeDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.chrome.ChromeDriver.startSession(ChromeDriver.java:181)
at org.openqa.selenium.remote.RemoteWebDriver.
at org.openqa.selenium.remote.RemoteWebDriver.
at org.openqa.selenium.chrome.ChromeDriver.
at org.openqa.selenium.chrome.ChromeDriver.
at org.openqa.selenium.chrome.ChromeDriver.
at Founder.testmvn.BaiduChromeDriver.main(BaiduChromeDriver.java:12)
******************************
chromedriver.exe当前版本为:version: '2.23.1', revision: '17143', time: '2012-06-08 18:59:28',该版本可能不支持当前最新chrome浏览器,或存在未修复的bug。
更换为最新版chromedriver.exe(2014/2/6版),重新运行该问题已经解决了。
===================================================================================================================
在Windows下使用FirefoxDriver的怪问题
1. 用FirefoxDriver写了个测试网页的程序,在Linux下运行正常,但在Windows下运行出错,报:Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: Bad request
搞了大半天,用了各种方式尝试解决,都没搞定,终于google找到了网上的一个文章关于这个的:http://seleniumforum.forumotion.net/t717-trouble-with-creating-an-firefoxdriver-object
居然通过修改C:\WINDOWS\system32\drivers\etc\HOSTS 文件,在其中添加上这句:
127.0.0.1 localhost
出现这个错误,很有意思。 查了一下 有人说应该是 hosts 出现了问题,加上一个 127.0.0.1 localhost 就行了,但我的 hosts 上肯定有这个玩意,为啥也会出现这个问题呢?
经过调试,发现 127.0.0.1 localhost 的设置必须要在 hosts 文件的最开始,而且如果后面有其他设置后,也不要再出现同样的 127.0.0.1 localhost ,只要有就会出错。(因为我为了方便访问 google 的网站,专门加入了 smarthosts 的内容,导致了 localhost 的重复)
【3. 测试 Chrome】=============================================================================
2. Exception in thread "main" org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms
Build info: version: '2.14.0', revision: '14955', time: '2011-11-29 11:43:45'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_16'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.internal.SocketLock.lock(SocketLock.java:95)
解决方案:
http://docs.seleniumhq.org/download/
selenium-server-standalone-2.33.0.jar
selenium-2.33.0.zip
选择最新的 jar 版本~重新加到工程里面~
===============================================================================================
考于: http://www.cnblogs.com/dream0577/archive/2012/10/07/2714579.html
FireFox浏览器可以直接使用下列代码就能打开。
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com.hk");
*************************************************************************************
IE需要设置,不然会提示错误。如果提示一下问题,请参照~~
1. 打开IE浏览器的代码如下:
public class FirstExampe {
public static void main(String[] args) {
System.setProperty("webdriver.ie.driver","C:\\Users\\li.chunmei\\Downloads\\IEDriverServer_Win32_2.33.0 (1)\\IEDriverServer.exe"); //问题2解决方法
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver ie_driver = new InternetExplorerDriver(ieCapabilities); //问题3解决方法
ie_driver.get("http://www.baidu.com");
String usr_url = ie_driver.getCurrentUrl();
System.out.println("User URL:" + usr_url);
}
log结果如下(
1. 对测试程序运行时的功能没有影响,作为一个warning):
Started InternetExplorerDriver server (32-bit)
2.33.0.0
Listening on port 41503
2013-7-18 14:54:50 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
2013-7-18 14:54:50 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: Retrying request
User URL : http://localhost:41503/
2. 提示下面的错误:
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.ie.driver system property; for more information, seehttp://code.google.com/p/selenium/wiki/InternetExplorerDriver. The latest version can be downloaded fromhttp://code.google.com/p/selenium/downloads/list
解决方法:需要设置IE的Driver到“webdriver.ie.driver”变量中
需要添加IEDriverServer.exe(从Selenium官网可下载的),并用如下的代码进行设置。
System.setProperty("webdriver.ie.driver","C:\\Users\\li.chunmei\\Downloads\\IEDriverServer_Win32_2.33.0 (1)\\IEDriverServer.exe");
3. 如果IE浏览器设置安全性较高,在“Internet Options”中都不要选择“Enable Protected Mode”(保护模式)!否则会提示下面的错误:
Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)
解决方法有两种,
一种是修改掉IE的设置,不要在任何情况下使用保护模式。
二种是前面代码中加入 如下片段在运行时设置IE的Capabilities:
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver ie_driver = new InternetExplorerDriver(ieCapabilities);
4.Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Browser zoom level was set to 89%. It should be set to 100% (WARNING: The server did not provide any stacktrace information)
错误原因:IE浏览器的比例调大了,按ctrl+0,可以恢复原来的大小,即可。PS:这种错误真是。。。让人无语
5.System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=8, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:36988/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=true, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: 6d1ba00b-a372-4598-95b4-a9e00c20ce2c
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
如果遇到上面的问题
解决方法有两种: 1.是修改掉IE的设置,不要在任何情况下使用保护模式(protected mode) 2.另一种即是前面代码中如下片段在运行时设置IE的Capabilities。 DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); ieCapabilities.setCapability (InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities);