下载驱动
http://chromedriver.storage.googleapis.com/index.html
加入依赖
1 2 3 4 5
| <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-remote-driver</artifactId> <version>3.0.1</version> </dependency>
|
代码实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Test public void get() throws Exception{ RemoteWebDriver driver = null; try { Capabilities capabilities = new DesiredCapabilities(); String remoteDriverUrl = "http://localhost:9515"; driver = new RemoteWebDriver(new URL(remoteDriverUrl),capabilities); TestUtil.sleep(30000); for (int i = 5640; i < 16000; i++) { driver.get("https://time.geekbang.org/column/article/"+i); TestUtil.sleep(3000); String pageSource = driver.getPageSource(); if (pageSource.contains("技术领导力300讲")){ System.out.println("https://time.geekbang.org/column/article/"+i); } } System.out.println(driver.getPageSource()); } catch (Exception e) { log.error("getDriver error:{}",e); }finally { TestUtil.sleep(2000); driver.quit(); } }
|