首先在eclipse中引入selenium的包,本实例用的是selenium-server-standalone-3.10.0,chromedriver2.35,以及chrome浏览器64.0.3282.186。注意chromedriver与chrome之间的版本匹配关系。
package com.datashow.util; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class DataCatch { public static void main(String[] args) { System.out.println("即将打开谷歌浏览器进行探索..."); System.setProperty("webdriver.chrome.driver", "D:\\Program_Files\\Chrome\\chromedriver.exe");//指定驱动路径 WebDriver driver = new ChromeDriver(); driver.get("http://www.baidu.com/"); String oo = driver.getCurrentUrl(); System.out.println(oo); WebElement keywords= driver.findElement(By.name("wd")); keywords.sendKeys("积德从善"); WebElement keysubmit=driver.findElement(By.id("su")); keysubmit.click(); System.out.println("已经成功打开谷歌浏览器..."); } } |