爬虫学习-selenium+chrome
来源:互联网 发布:淘宝购物车结算50件 编辑:程序博客网 时间:2024/06/03 19:34
(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());// 让浏览器访问 Baidudriver.get("http://www.baidu.com");// 用下面代码也可以实现// driver.navigate().to("http://www.baidu.com");// 获取 网页的 titleSystem.out.println(" Page title is: " + driver.getTitle());// 通过 id 找到 input 的 DOMWebElement element = driver.findElement(By.id("kw1"));// 输入关键字element.sendKeys("zTree");// 提交 input 所在的 formelement.submit();// 通过判断 title 内容等待搜索页面加载完毕,间隔秒(new WebDriverWait(driver, 10)).until(new ExpectedCondition() {@SuppressWarnings("unused")public Boolean apply(WebDriver d) {return d.getTitle().toLowerCase().startsWith("ztree");}@Overridepublic Object apply(Object arg0) {// TODO Auto-generated method stubreturn null;}});// 显示搜索结果页面的 titleSystem.out.println(" Page title is: " + driver.getTitle());// 关闭浏览器driver.quit();// 关闭 ChromeDriver 接口service.stop();}}
阅读全文
0 0
- 爬虫学习-selenium+chrome
- Selenium+Chrome(PhantomJs) Python爬虫
- 记一次python+selenium+chrome爬虫
- 爬虫学习笔记--Selenium PhantomJS
- Linux 使用selenium chrome实现爬虫的两个坑
- 基于selenium-java封装chrome、firefox、phantomjs实现爬虫
- python爬虫,selenium使用,Firefox-chrome-IE问题解决方法,selenium的基本操作
- Python爬虫学习之Selenium详解
- [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图
- python+selenium调用浏览器(IE-Chrome-Firefox)实现爬虫功能
- 爬虫学习--基于selenium动态抓捕(一)安装
- 爬虫学习笔记--用selenium 爬数据到Mysql
- Python3爬虫学习笔记(5.Selenium简单操作介绍)
- 第二个selenium-chrome,
- selenium调用chrome
- Selenium Chrome启动
- Selenium - Drive Chrome
- selenium 安装+ 调用chrome
- Android 图片压缩
- 文章标题
- NUC1077 Humble Numbers【数学计算+打表】
- ACCESS数据库时间应用之按时间段对字段进行分组
- 人工智能泰斗迈克尔·乔丹分享机器学习要义:创新视角,直面挑战
- 爬虫学习-selenium+chrome
- NUC1421 时间日期格式转换【日期计算】
- 子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次,请写出程序
- 2017.06.21日作业
- 图的遍历算法详解
- [out/target/product/rk3288/obj/GYP/shared_intermediates/ui/gl/jni/Surface_jni.h] Error 1
- caffe + windows + python + gpu 配置
- servlet,filter,listener三者的加载顺序
- 20170622《指导生活的算法》