selenium介绍
selenium是一系列基于Web的自动化工具,提供一套测试函数,用于支持Web自动化测试。函数非常灵活,能够完成界面元素定位、窗口跳转、结果比较。可以借助这一功能进行渲染数据的抓取。
1.渲染抓取的由来
由于ajax的出现,一些网站的页面都是页面空,壳加上异步填充数据,普通的爬虫只能抓取静态页面,对于异步的只能抓取空壳。
2.抓取原理
1.java程序调用chrome驱动。
2.驱动调用无界面浏览器,进行抓取渲染后的据。
3.chromeDriver的安装(一定要注意chrome浏览器和chromeDriver的版本需要保持一致)
1.驱动下载路径:https://chromedriver.storage.googleapis.com/
windows下安装
下载chrome浏览器及运行程序时指定chromeDriver路径即可。
linux下安装
1.必须CentOS7.X及以上,查看 版本命令 cat /etc/redhat-release
2.安装Chrome浏览器
vim /etc/yum.repos.d/google-chrome.repo
添加以下内容
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64
enabled=1
gpgcheck=1
gpgkey=https://dl.google.com/linux/linux_signing_key.pub
保存后执行
yum -y install google-chrome-stable --nogpgcheck
3.安装chromeDriver及解压
wget https://chromedriver.storage.googleapis.com/78.0.3904.105/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
4.版本问题
查看chrome浏览器版本命令:google-chrome -version
查看chromeDriver版本命令:先切换到chromedriver目录下执行 ./chromedriver -version
两者的版本需要一样,否则会启动报错。
缺点
1.等待渲染时间不好控制。
2.性能比较差,建议建立chromeDriver连接池。连接池数量过多会导致chromeDriver进程卡死。
3.测试的一些参数:url=https://film.qq.com/film_all_list/allfilm.html?type=movie,休眠时间都为1s.
2核4g,chromeDriver数为12最佳,qps=8
4核8g,chromeDriver数为17最佳,qps=10
java代码调用
pom依赖
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
package com.yicong.boke.test;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
/**
* @Description:
* @Author yicong
* @Date 2020年5月18日
*/
public class SeleniumTest {
static String chromeDriverPath = "D:\\My Documents\\Downloads\\chromedriver_win32\\chromedriver.exe";
// String chromeDriverPath = "/home/search/chrome/chromedriver";
private static ChromeDriver initChromeDriver() {
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Nexus 5");
ChromeOptions options = new ChromeOptions();
// 不加载图片, 提升速度
options.addArguments("blink-settings=imagesEnabled=false");
// 谷歌文档提到需要加上这个属性来规避bug
options.addArguments("disable-gpu");
options.addArguments("disable-plugins");
// 禁用java
options.addArguments("disable-java");
// 以最高权限运行
options.addArguments("no-sandbox");
// 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
// options.addArguments("headless");
options.setExperimentalOption("mobileEmulation", mobileEmulation);
ChromeDriver webDriver = new ChromeDriver(options);
return webDriver;
}
public static void main(String[] args) {
// 加载chromeDriver的路径
System.getProperties().setProperty("webdriver.chrome.driver", chromeDriverPath);
// 初始化一个ChromeDriver
ChromeDriver chromeDriver = initChromeDriver();
String url = "http://book.sina.com.cn/excerpt/rwws/";
chromeDriver.get(url);
// 等待页面加载
// 1.休眠时间,强行等待渲染
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
//2.隐试等待 1秒
// chromeDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//3.显示等待时间10s,默认每隔0.5s检测一次当前的页面class=item-text这个元素是否存在
WebDriverWait w=new WebDriverWait(chromeDriver,10);
w.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("item-text")));
// 获取页面源码
String html = chromeDriver.getPageSource();
// 标题
// String title = chromeDriver.getTitle();
// String currentUrl = chromeDriver.getCurrentUrl();
System.out.println(html);
chromeDriver.close();
}
}
如报错,则说明chromeDriver路径不对
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html