0
点赞
收藏
分享

微信扫一扫

【Java爬虫】009-Selenium学习笔记


一、概述

1、简介

Selenium是一个用于Web应用程序测试的工具;
Selenium测试直接运行在浏览器中,就像真正的用户在操作一样;

 

2、主要功能

测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上;
测试系统功能——创建回归测试检验软件功能和用户需求;
支持自动录制动作自动生成.Net、Java、Perl等不同语言的测试脚本

 

3、Maven坐标

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>

 

4、谷歌浏览器驱动下载地址(国内)

​​https://npm.taobao.org/mirrors/chromedriver​​

 

二、入门程序

第一步:导入Maven坐标

第二步:下载自己的谷歌浏览器对应版本的驱动win32是windows通用;

第三步:写代码

package com.zb.book.selenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
public static void main(String[] args) {
//设置驱动位置
System.setProperty("webdriver.chrome.driver", "D:\\MySoft\\ChromeDriver\\chromedriver.exe");
//创建一个谷歌浏览器对象
WebDriver driver = new ChromeDriver(); //Chrome浏览器
//访问百度首页
driver.get("http://www.baidu.com/");
//获取标题,并打印
System.out.println(driver.getTitle());
//关闭浏览器
driver.close();
}
}

运行结果:

系统自动启动了谷歌浏览器,并访问了百度首页,获取标题之后,关闭了浏览器;

【Java爬虫】009-Selenium学习笔记_System

 

三、增强入门程序

1、需求

实现使用自动搜索关键词“猫”;

 

2、代码演示

package com.zb.book.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
public static void main(String[] args) {
//设置驱动位置
System.setProperty("webdriver.chrome.driver", "D:\\MySoft\\ChromeDriver\\chromedriver.exe");
//创建一个谷歌浏览器对象
WebDriver driver = new ChromeDriver(); //Chrome浏览器
//访问百度首页
driver.get("http://www.baidu.com/");
//获取标题,并打印
System.out.println(driver.getTitle());
//定位元素,并设置内容
driver.findElement(By.cssSelector("#kw")).sendKeys("猫");
//定位元素,并点击
driver.findElement(By.cssSelector("#su")).click();
//关闭浏览器
//driver.close();
}
}

 

3、运行结果

【Java爬虫】009-Selenium学习笔记_java_02

 

四、关于元素定位的说明

元素定位的方法有很多,使用也很简单,比如通过id、标签名、css选择器、Xpath等等,如下图:

【Java爬虫】009-Selenium学习笔记_selenium_03

 

五、下拉滚动条

1、代码演示

package com.zb.book.selenium;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
public static void main(String[] args) throws InterruptedException {
//设置驱动位置
System.setProperty("webdriver.chrome.driver", "D:\\MySoft\\ChromeDriver\\chromedriver.exe");
//创建一个谷歌浏览器对象
WebDriver driver = new ChromeDriver(); //Chrome浏览器
//访问百度首页
driver.get("https://www.youku.com/category/show/c_96_s_1.html?spm=a2hcb.12701310.app.5~5!2~5!2~5~5~DL!7~DD~A");
//获取标题,并打印
System.out.println(driver.getTitle());
//睡眠5秒,下拉
Thread.sleep(5000);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scrollTo(0,5000)");
System.out.println(1);
Thread.sleep(5000);
js.executeScript("scrollTo(5000,10000)");
System.out.println(2);
Thread.sleep(5000);
js.executeScript("scrollTo(10000,30000)");
System.out.println(3);
String html = driver.getPageSource();
System.out.println(html);
//关闭浏览器
driver.close();
}
}

 

2、运行结果

【Java爬虫】009-Selenium学习笔记_chrome_04

 

六、其它

其它有隐藏浏览器、截取验证码等功能,用时再细细研究。


 

 

 

举报

相关推荐

0 条评论