selenium java IE问题 && Firefox问题 && chrome问题

来源:互联网 发布:c语言学生成绩管理 编辑:程序博客网 时间:2024/04/19 12:03

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)>

Java代码 复制代码 收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.chrome.ChromeDriver;  
  5.   
  6. public class BaiduChromeDriver {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.         // TODO Auto-generated method stub  
  13.     //设置访问ChromeDriver的路径  
  14. System.setProperty("webdriver.chrome.driver""C:\\Documents and Settings\\Administrator\\Local Settings\\
  15. Application Data\\Google\\Chrome\\Application\\chromedriver.exe");   
  16.         WebDriver driver = new ChromeDriver();  
  17.         driver.get("http://www.baidu.com/");  
  18.   
  19.     }  
  20.   
  21. }  


ps:
chrome浏览器安装时默认路径如下:

OSExpected Location of ChromeLinux/usr/bin/google-chrome1Mac/Applications/Google\ Chrome.app/Contents/MacOS/Google\ ChromeWindows XP%HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exeWindows VistaC:\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.<init>(RemoteWebDriver.java:126)
 at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:139)
 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:160)
 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:149)
 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:106)
 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


代码:


import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class WebTest{
private WebDriver webdriver;
private String url;
@Before
public void setUp() throws Exception {
webdriver =  new FirefoxDriver();
url = "http://www.baidu.com";
webdriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

@After
public void tearDown() throws Exception {
webdriver.quit();
}

@Test
public void testWebTest() throws Exception  {
webdriver.get(url+"/");
WebElement element = webdriver.findElement(By.id("kw"));
element.clear();
element.sendKeys("test");
WebElement submitElement = webdriver.findElement(By.id("su"));
submitElement.click();
}

}
=============================================================================
错误提醒:http://my.oschina.net/dyhunter/blog/94090

1)Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.
出现这个错误,是说明你的 FireFox 文件并没有安装在默认目录下,这时候需要在最开始执行:System.setProperty 设置环境变量  "webdriver.firefox.bin" 将自己机器上 FireFox 的正确路径设置完毕后即可。
2)Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: Bad request

出现这个错误,很有意思。 查了一下 有人说应该是 hosts 出现了问题,加上一个 127.0.0.1  localhost 就行了,但我的 hosts 上肯定有这个玩意,为啥也会出现这个问题呢? 

经过调试,发现 127.0.0.1 localhost 的设置必须要在 hosts 文件的最开始,而且如果后面有其他设置后,也不要再出现同样的 127.0.0.1 localhost ,只要有就会出错。(因为我为了方便访问 google 的网站,专门加入了 smarthosts 的内容,导致了 localhost 的重复)

【3. 测试 Chrome】
Chrome 虽然不是 Selenium 的原配,但是没办法,她太火辣了,绝对不能抛下她不管的。
把 ExampleForFireFox.java 稍微修改就可以制作出一个 ExampleForChrome.java ,直接把 new FireFoxDriver() 修改为 new ChromeDriver() 你会发现还是行不通。

错误如下:
1)Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
这应该是找不到 chrome 的文件,好吧,利用 System.setProperty 方法添加路径,这里要注意,是 “webdriver.chrome.driver” 可不是“webdriver.chrome.bin

设置路径后还是会报错:
2)[6416:4580:1204/173852:ERROR:gpu_info_collector_win.cc(91)] Can't retrieve a valid WinSAT assessment.
这个貌似是因为 Selenium 无法直接启动 Chrome 导致的,必须要通过前面咱们下载 Chrome 的第三方插件 ChromeDriver,去看第一个错误中提示给你的 网址:http://code.google.com/p/selenium/wiki/ChromeDriver
按照人家给的例子来修改我们的测试代码吧:

=============================================================================

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);










0 0
原创粉丝点击