| 
 
 目录  
一、Selenium简介?  
二、Selenium组成  
三、Selenium特点  
四、案例演示  
1、首先创建一个项目,命名随意,导入Selenium的pom依赖  
2、下载驱动包  
五、?Selenium爬取商品信息  
1)初始化  
2)点开网址并指定关键字搜索?  
3)设定睡眠时间(可根据网络速度实际调整)  
4)查找商品列表并获取相关信息  
?六、Selenium爬取图片  
1)保存图片?  
 
 
一、Selenium简介? 
Selenium是一个用于Web应用程序自动化测试工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。  适用于自动化测试,js动态爬虫(破解反爬虫)等领域  
二、Selenium组成 
1)Selenium IDE:嵌入到Firefox浏览器中的一个插件,实现简单的浏览器操作录制与回放功? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 能,主要用于快速创建BUG及重现脚本,可转化为多种语言  2)Selenium RC: 核心组件,支持多种不同语言编写自动化测试脚本,通过其服务器作为代理? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 服务器去访问应用,达到测试的目的  3)Selenium WebDriver(重点):一个浏览器自动化框架,它接受命令并将它们发送到浏览器? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?它是通过特定于浏览器的驱动程序实现的。它直接与浏览器通? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?信并对其进行控制。Selenium?WebDriver支持各种编程语? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?言,如Java、C# 、PHP、Python、Perl、Ruby  4)Selenium grid:测试辅助工具,用于做分布式测试,可以并行执行多个测试任务,提升测试效率  
三、Selenium特点 
? ? ?1)开源、免费  
? ? ?2)多浏览器支持:FireFox、Chrome、IE、Opera、Edge;  
? ? ?3)多平台支持:Linux、Windows、MAC;  
? ? ?4)多语言支持:Java、Python、Ruby、C#、JavaScript、C++;  
? ? ?5)对Web页面有良好的支持;  
? ? ?6)简单(API 简单)、灵活(用开发语言驱动);  
?????7)支持分布式测试用例执行  
四、案例演示 
1、首先创建一个项目,命名随意,导入Selenium的pom依赖 
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
     </dependency>  
2、下载驱动包 
下载网址:http://chromedriver.storage.googleapis.com/index.html  
小编使用谷歌访问Selenium,下载驱动包前先查看一下你的谷歌是哪个版本的后在下载网址中找到对应的版本下载  
   
?   
  ? ?解压-->? ? ? 解压后将它拷贝到一个非中文目录下,利用驱动程序去驱动浏览器  
?  
 
 固定代码:
//设置驱动
System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");
//创建驱动
WebDriver driver=new ChromeDriver();
//与将要爬取的网站建立连接
driver.get("https://www.baidu.com"); 
  
   
运行一下,测试百度网站?  
 
   
1)Class选择:driver.findElement(By.className("s_ipt"));
//      通过类选择器拿到被控制的页面的按钮元素
        WebElement s_btn = driver.findElement(By.className("s_btn"));
        System.out.println(s_btn.getAttribute("id"));
        System.out.println(s_btn.getAttribute("value"));  
   
2)ID选择:   driver.findElement(By.id("kw"));
//        通过id选择器拿到页面中的元素
        WebElement su = driver.findElement(By.id("su"));
        System.out.println(su.getAttribute("class"));  
   
 
 //      3)name选择: driver.findElement(By.name("wd"));
//        ????????System.out.println(driver.findElement(By.name("rqlang")).getAttribute("value"));
//      4)tag选择:  driver.findElements(By.tagName("input"));
//        获取到百度首页所有点击选择
        List<WebElement> eles = driver.findElements(By.tagName("a"));
        for (WebElement ele : eles) {
            //ele指的是单个a标签
            String text = ele.getText();
            if (text != null && !"".equals(text.trim())){
                System.out.println(text);
            }
        }
 
  
   
 
 ?  
 5)link选择: driver.findElement(By.linkText("地图"));
//        通过链接文本获取链接元素,模拟点击该链接
//        driver.findElement(By.linkText("地图")).click();
//      6)Partial link选择(a标签文本内容模糊匹配)driver.findElement(By.partialLinkText("使用百"));
//        通过链接的地址模糊匹配
        List<WebElement> eles = driver.findElements(By.partialLinkText("3"));
        for (WebElement ele : eles) {
            System.out.println(ele.getText());
        } 
  
?   
 
 ?  
 7)css选择器:driver.findElement(By.cssSelector("#kw"));
        //通过CSS选择器获取页面元素
//        WebElement ele = driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(1)"));
//        System.out.println(ele.getText()); 
  
   
 
 ?  
 8)xpath选择:driver.findElement(By.xpath("//*[@id=\"kw\"]"));
//        /students/student/...
        WebElement ele = driver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[1]"));
        System.out.println(ele.getText()); 
  
 ?  
五、?Selenium爬取商品信息 
?1)初始化 
 
 //将驱动加载到Java的JVM虚拟机中  
 System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");  
   /***********************************方式一:不打开浏览器**************************/  
 //定义浏览器参数  
 ChromeOptions chromeOptions = new ChromeOptions();  
   //设置不打开浏览器  
 chromeOptions.addArguments("--headless");  
   //初始化驱动  
 driver=new ChromeDriver(chromeOptions);  
   /***********************************方式二:打开浏览器**************************/  
 //初始化驱动  
 WebDriver driver=new ChromeDriver();  ?  
  
2)点开网址并指定关键字搜索? 
 
 //与将要爬取的网站建立连接  driver.get("https://search.jd.com/search?keyword=%E6%89%8B%E6%9C%BA&wq=%E6%89%8B%E6%9C%BA&cid3=13768");  
   //输入关键字  
 ? driver.findElement(By.id("key")).sendKeys("衣服");  
  
 //点击搜索按钮  ? driver.findElement(By.cssSelector("button.button")).click();  
  
3)设定睡眠时间(可根据网络速度实际调整) 
 
 Thread.sleep(i * 1000);  
  
4)查找商品列表并获取相关信息 
 
 //1.获取到 包含 手机 所有信息 的页面元素 div  List<WebElement> divs = driver.findElements(By.className("gl-i-wrap"));  System.out.println(divs.size());  
 for (WebElement div : divs) {  ? ? //2.通过 div 获取 具体的爬取信息 信息 价格 图片  ? ? System.out.println(div.findElement(By.cssSelector("div.p-name > a > em")).getText());  ? ? System.out.println(div.findElement(By.cssSelector("div.p-price > strong > i")).getText());  ? ? WebElement img = div.findElement(By.cssSelector("div.p-img > a > img"));  ? ? System.out.println(img.getAttribute("src"));  }  
  
   
?六、Selenium爬取图片 
1)保存图片? 
 
 URL url=new URL(img);
//创建输入流
InputStream is=url.openStream();
//创建输出流
OutputStream out=new FileOutputStream(new File(路径));  
  
package com.xiaokun.demo;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;
import java.util.UUID;
 
/**
 *
 * 目标:手机信息、价格、图片
 * 步骤:
 *  1.获取到 包含 手机 所有信息 的页面元素 div
 *  2.通过 div 获取 具体的爬取信息
 *  信息
 *  价格
 *  图片
 *  3.针对图片做针对的处理——>从网上将图片下载下来
 *      3.1 将 源头图片 定义 输入流
 *      3.2 定义 最终图片下载订制,定义 输出流
 *      3.3 边读边写
 *      3.4 释放资源
 */
public class Demo2 {
 
    public static void main(String[] args) throws Exception {
        //设置驱动
        System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");
        //创建驱动
        WebDriver driver=new ChromeDriver();
        //与将要爬取的网站建立连接
        driver.get("https://search.jd.com/search?keyword=%E6%89%8B%E6%9C%BA&wq=%E6%89%8B%E6%9C%BA&cid3=13768");
 
        //1.获取到 包含 手机 所有信息 的页面元素 div
        List<WebElement> divs = driver.findElements(By.className("gl-i-wrap"));
        System.out.println(divs.size());
 
        for (WebElement div : divs) {
            //2.通过 div 获取 具体的爬取信息 信息 价格 图片
            System.out.println(div.findElement(By.cssSelector("div.p-name > a > em")).getText());
            System.out.println(div.findElement(By.cssSelector("div.p-price > strong > i")).getText());
            WebElement img = div.findElement(By.cssSelector("div.p-img > a > img"));
            System.out.println(img.getAttribute("src"));
 
            //3.针对图片做针对性的处理--->从网上将图片下载下来
            downloadImg(img.getAttribute("src"));
            System.out.println("===========大家都在发===嘿嘿 苦笑 呲牙 强颜欢笑 憨笑=====");
        }
 
        //关闭浏览器
        driver.close();
        //释放资源
        driver.quit();
 
 
    }
 
   
    private static void  downloadImg(String src) throws Exception{
        if(null == src || "".equals(src))
            return;
        //3.1 将源头图片 定义 输入流
        URL url = new URL(src);
        InputStream in = url.openStream();
        //3.2 定义 最终图片下载定制,定义 输出流
        OutputStream out = new FileOutputStream(new File("E:\\temp\\"+ UUID.randomUUID().toString()+".jpg"));
        //3.3 边读边写
        int len = 0;//一次读多少字节
        byte[] bytes = new byte[1024];//默认一次读1k
        while ((len = in.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
        //3.4 释放资源
        in.close();
        out.close();
    }
 
}  
   
本地路径可以看到下载后的图  
 ?  
?  
 
浅浅了解看这篇够了,别杠👀👀👀  
 
 
 
 
                
        
        
    
  
 
 |