Selenium

一、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、下载驱动包

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、入门

package com.xnx.demo;

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

import java.util.List;

/**
 * @author xnx
 * @create 2022-09-28 17:06
 */
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"));
//        通过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(ele.getText());
//        }

//        5)link选择: driver.findElement(By.linkText("地图"));
//        通过链接文本获取元素,模拟点击链接
//        driver.findElement(By.linkText("地图")).click();

//        6)Partial link选择(a标签文本内容模糊匹配):driver.findElement(By.partialLinkText("使用百"));
//        通过获取到所有包含3的链接地址
//        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(3) > a > span.title-content-title"));
//        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[5]/a/span[2]"));
        System.out.println(ele.getText());
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值