2014-02-20 94 views
0

我試圖用Selenium Webdriver啓動Chrome和使用下面的代碼:如何在Selenium webdriver中打開chrome?

System.setProperty("webdriver.chrome.driver", 
       "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); 
WebDriver driver=new ChromeDriver(); 
driver.get("http://www.yahoo.com"); 

Chrome瀏覽器打開,但沒有進一步進行。可能是以下錯誤的原因:

Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 

回答

-1

使用最新版本的ChromeDriver。

來源|

http://chromedriver.storage.googleapis.com/index.html 
+0

我認爲zip文件可用於32位,但我使用的是64位機器。無論如何,我下載仍然得到相同的錯誤 – Shyamala

+0

@Shyamala似乎目前沒有WinD64位的ChromeDriver。 [不知道,可能是Win-32 bit works] –

+0

您是否嘗試過我選擇複選框的解決方案?嘗試我的用例,而不是Thread.sleep() –

1

您錯誤地啓動驅動器

webdriver.chrome.driver應該是路徑司機您已經下載並 Chrome的物理位置。

+0

好吧,我現在已經下載並提供瞭如下路徑:System.setProperty(「webdriver.chrome.driver」,「C:/ Program Files(x86)/Google/chromedriver.exe」); \t \t WebDriver driver = new ChromeDriver(); \t \t driver.get(「http://www.yahoo.com」);但現在Chrome已經打開並輸入數據;並顯示以下錯誤在端口21321上啓動ChromeDriver(v2.4.226107),並且端口號每次都會更改 – Shyamala

+0

兩件事情a)端口號將發生變化,b)這不是最新版本。 – Arran

1

首先,您需要從此鏈接下載chrome驅動程序文件,然後將它的JAR導入到eclipse中的包中。

Download the link from here

然後,你將不得不進口它在你的程序。

import org.openqa.selenium.chrome.ChromeDriver; 

比作驅動程序實例

driver = new ChromeDriver(); 

下載Chrome的外部JAR

在Eclipse ::右鍵單擊相應的包裝物(在Package Explorer),然後點擊屬性。轉到Java構建路徑並添加外部jar。現在添加chrome的jar文件。並且按照我在ans中寫入的步驟導入鉻驅動程序並創建實例

請按照照片中的這些步驟進行操作。 1)

從這裏選擇您的文件,然後右擊 enter image description here

+0

但是當我從這個鏈接下載並解壓縮時,只有EXE文件有 – Shyamala

+0

或者你也可以從這裏下載,http://www.java2s.com/Code/Jar/s/Downloadseleniumchromedriver20a4jar.htm –

+0

OK我下載了並添加爲外部jar ..... System.setProperty(「webdriver.chrome.driver」,「C:/ Program Files(x86)/Google/Chrome/Application/chrome.exe」);並得到錯誤如下[4236:2696:0220/181910:錯誤:chrome_views_delegate.cc(176)]未執行 [4236:2696:0220/181910:錯誤:desktop_root_window_host_win.cc(746)]未實現 – Shyamala

0

您需要設置瀏覽器設置第一個。如果有幫助,請嘗試下面提到的代碼:

public void setup()  
{   
    System.setProperty("webdriver.chrome.driver", "C:\\**PATH**\\chromedriver.exe"); 
    ChromeOptions options = new ChromeOptions(); 
    options.addArguments("test-type"); 
    options.addArguments("start-maximized"); 
    options.addArguments("--js-flags=--expose-gc"); 
    options.addArguments("--enable-precise-memory-info"); 
    options.addArguments("--disable-popup-blocking"); 
    options.addArguments("--disable-default-apps"); 
    options.addArguments("test-type=browser"); 
    options.addArguments("disable-infobars"); 
    driver = new ChromeDriver(options); 
    driver.manage().deleteAllCookies(); 
} 

您需要通過懸停在錯誤行上來導入文件。

相關問題