Selenium轻松入门!

目录

 一、Selenium简介

      二、Selenium组成

1)Selenium IDE:

2)Selenium RC:

3)Selenium WebDriver(重点):     

4)Selenium grid:

 三、Selenium特点

 四、案例演示

     4.1 java爬虫入门

     1.下载驱动包

     2.创建项目并导入依赖

 4.3.入门

 4.2 相关API

    1.元素选择方式

     1)Class选择:driver.findElement(By.className("s_ipt"));


 一、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)支持分布式测试用例执行。

 四、案例演示

 爬虫:数据采集、数据清晰、数据分析!!!

     4.1 java爬虫入门

     1.下载驱动包

     http://chromedriver.storage.googleapis.com/index.html

 

 

下载解压

 

     2.创建项目并导入依赖

     <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
     </dependency>

 3.入门

     //设置驱动
     System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
     //创建驱动
     WebDriver driver=new ChromeDriver();
     //与将要爬取的网站建立连接
     driver.get("https://www.baidu.com");
     //关闭浏览器
     driver.close();
     //释放资源
     driver.quit();

 

 

 4.2 相关API

    1.元素选择方式


     1)Class选择driver.findElement(By.className("s_ipt"));

package com.chenchen.demo;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
/**
 * @author 杨总
 * @create 2022-09-29 09:50
 */
public class Demo1 {
    public static void main(String[] args) {
        //设置驱动
        System.setProperty("webdriver.chrome.driver","D:\\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"));

  2)ID选择:   driver.findElement(By.id("kw"));
        WebElement su = driver.findElement(By.id("su"));
        System.out.println(su.getAttribute("class"));

 

   3)name选择 driver.findElement(By.name("wd"));

 

//        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("使用百度"));

//     通过3获取所有包含3的链接地址
        List<WebElement> eles = driver.findElement(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("#kw"));
        System.out.println(ele.getText());

     8)xpath选择:driver.findElement(By.xpath("//*[@id=\"kw\"]"));
 

WebElement ele = driver.findElement(By.xpath("//*[@id=\"kw\"]"));
        System.out.println(ele.getText());
 //拿到百度首页的input输入框
        WebElement search_input = driver.findElement(By.xpath("//*[@id=\"kw\"]"));
 
//        往 输入框 输入搜索关键字
        search_input.sendKeys("禁止收敛URL");
//      模拟等待
        Thread.sleep(3000);
 
//        获取百度一下的点击按钮
        driver.findElement(By.id("su")).click();

2.获取单个元素:driver.findElement
3.获取多个元素:driver.findElements
4.输入内容:input.sendKeys("java");
5.元素点击:element.click();
6.获取元素属性:nextPageEle.getAttribute("class")
7.获取标签文本内容:titleEle.getText()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值