爬蟲學習-selenium+chrome
阿新 • • 發佈:2018-12-26
(1)安裝 chrome 瀏覽器
(2)下載
(3)第一個selenium程式
必要的jar包
試驗程式碼
import java.io.File; import java.io.IOException; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class ExampleForChrome { public static void main(String[] args) throws IOException, InterruptedException { new ExampleForChrome().testOtherSearch(); } public void testOtherSearch() throws InterruptedException { // Optional, if not specified, WebDriver will search your path for // chromedriver. System.setProperty("webdriver.chrome.driver", "D:/軟體備份/測試/chromedriver_win32/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.newrank.cn/public/info/list.html?period=week&type=data"); Thread.sleep(5000); WebElement more = driver.findElement(By.cssSelector("p.showmore a")); more.click(); Thread.sleep(5000); // Let the user actually see something! List<WebElement> names = driver.findElements(By.cssSelector("table tbody tr td h4 a")); for (WebElement e : names) { System.out.println(e.getText()); } Thread.sleep(5000); // Let the user actually see something! driver.quit(); } @SuppressWarnings({ "unchecked", "rawtypes" }) public void testBaiduSearch() throws IOException { // 設定 chrome 的路徑(如果你安裝chrome的時候用的預設安裝路徑,則可省略這步) System.setProperty("webdriver.chrome.driver", "C:/Users/宇翔/AppData/Local/Google/Chrome/Application/chrome.exe"); // 建立一個 ChromeDriver 的介面,用於連線 Chrome(chromedriver.exe // 的路徑可以任意放置,只要在newFile()的時候寫入你放的路徑即可) ChromeDriverService service = new ChromeDriverService.Builder() .usingDriverExecutable(new File("D:/軟體備份/測試/chromedriver_win32/chromedriver.exe")).usingAnyFreePort() .build(); service.start(); // 建立一個 Chrome 的瀏覽器例項 WebDriver driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome()); // 讓瀏覽器訪問 Baidu driver.get("http://www.baidu.com"); // 用下面程式碼也可以實現 // driver.navigate().to("http://www.baidu.com"); // 獲取 網頁的 title System.out.println(" Page title is: " + driver.getTitle()); // 通過 id 找到 input 的 DOM WebElement element = driver.findElement(By.id("kw1")); // 輸入關鍵字 element.sendKeys("zTree"); // 提交 input 所在的 form element.submit(); // 通過判斷 title 內容等待搜尋頁面載入完畢,間隔秒 (new WebDriverWait(driver, 10)).until(new ExpectedCondition() { @SuppressWarnings("unused") public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("ztree"); } @Override public Object apply(Object arg0) { // TODO Auto-generated method stub return null; } }); // 顯示搜尋結果頁面的 title System.out.println(" Page title is: " + driver.getTitle()); // 關閉瀏覽器 driver.quit(); // 關閉 ChromeDriver 介面 service.stop(); } }