如圖所示,運行時打開瀏覽器出現data,這意味着chromedriver與selenium版本不符。隨着瀏覽器更新,對應版本會一直更新。用的時候搜一下即可。文章末尾提供了驅動器的下載地址。
異常信息:Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.SocketException: Connection reset
我是配置了2.40的環境在path中,但還是有這個問題。只能在代碼中指定驅動器的路徑,將下列代碼加在實例化驅動之前即可。
System.setProperty("webdriver.chrome.driver", "C:\\Users\\anyan\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
以下提供我的測試代碼實例:
import junit.framework.Assert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumBuildExample { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "C:\\Users\\anyan\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"); WebDriver webDriver=new ChromeDriver(); webDriver.navigate().to("http://www.baidu.com"); WebElement search_input=webDriver.findElement(By.name("wd")); search_input.sendKeys(時間"); search_input.submit(); Thread.sleep(3000); Assert.assertEquals("時間_百度搜索",webDriver.getTitle()); webDriver.quit(); } }
驅動下載地址:http://chromedriver.storage.googleapis.com/index.html