爬虫学习-selenium+chrome

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-29 09:59   531   0

(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();
 }

}


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP