selenium常用API的使用

这篇具有很好参考价值的文章主要介绍了selenium常用API的使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

定位元素(findElement)

CSS选择器定位 

xpath选择器定位 

CSS选择器和xpath选择器区别 

操作测试对象 

click:点击

sendKeys:输入文本框内容

clear:清除文本框内容 

text:获取元素文本内容

getText()(获取一般文本内容)

getAttribute() (获取属性(value))

submit:提交

submit和click的区别 


定位元素(findElement)

CSS选择器定位 

首先找到指定的页面,找到后确认要定位的元素:如百度搜索首页的搜索框 

selenium常用API的使用,测试,selenium,测试工具打开开发者工具,点击元素:

selenium常用API的使用,测试,selenium,测试工具 找到对应的类,将类名复制下来,调用webdriver的findElement方法,通过By将类传入(使用css选择器) 

WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));

在文本框中输入使用sendKeys方法即可:

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

public class Main {
    public static void main(String[] args) {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        //定位元素
        //找到百度搜索框
       WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
       //输入软件测试
        element.sendKeys("软件测试");

    }
}

 点击运行后会先打开Chrome浏览器百度搜索页面,之后在文本框输入“软件测试”进行搜索

selenium常用API的使用,测试,selenium,测试工具

更多使用css选择器的方法:(了解)

id选择器(#id)

selenium常用API的使用,测试,selenium,测试工具

类选择(class)

selenium常用API的使用,测试,selenium,测试工具

标签选择(标签名)

selenium常用API的使用,测试,selenium,测试工具

后代选择 (父级选择器,子级选择器)等等

xpath选择器定位 

xpath选择器定位和css选择器定位类似,右键标签进行复制xpath 

selenium常用API的使用,测试,selenium,测试工具

WebElement element=webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));

xpath定位的几个用法:

绝对路径(/html/head/title(不常用))

selenium常用API的使用,测试,selenium,测试工具

相对路径 

相对路径+索引(//form/span[1]/input)

selenium常用API的使用,测试,selenium,测试工具

相对路径+属性值(//input[@class="s_ipt"])

selenium常用API的使用,测试,selenium,测试工具

相对路径+通配符 (//*[@*="su"])

selenium常用API的使用,测试,selenium,测试工具

相对路径+文本匹配(//a[text()="新闻"])

selenium常用API的使用,测试,selenium,测试工具

CSS选择器和xpath选择器区别 

首先css选择器效率比xpath选择器高(css是和html捆绑的,xpath是和DOM捆绑的(DOM是js和html的中介)) 

其次xpath功能上要比css更强大,比如xpath可以选中多个元素,支持文本定位和上下移动。

操作测试对象 

selenium中操作测试对象常用的有以下几种:

click:点击

如在百度搜索框输入“软件测试”后点击百度一下:

 WebElement element=webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //输入软件测试
        element.sendKeys("软件测试");
        //找到百度一下按钮
        //进行点击
        webDriver.findElement(By.cssSelector("#su")).click();

sendKeys:输入文本框内容

如在百度搜索框输入“软件测试” 

WebElement element=webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //输入软件测试
        element.sendKeys("软件测试");

clear:清除文本框内容 

如在百度搜索框中输入“软件测试”后点击百度一下,然后将文本框清除,输入“前端vue”进行搜索

private static void test02() throws InterruptedException {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.findElement(By.cssSelector("#kw")).clear();
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("前端vue");
        webDriver.findElement(By.cssSelector("#su")).click();

    }

text:获取元素文本内容

getText()(获取一般文本内容)

如获取百度搜索首页的百度热搜第一条:

selenium常用API的使用,测试,selenium,测试工具

    private static void test03() {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        //获取到百度热搜第一条的内容
        String res = webDriver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[1]/a/span[2]"))
                .getText();
        System.out.println(res);


    }

selenium常用API的使用,测试,selenium,测试工具

getAttribute() (获取属性(value))

 在getAttribute当中传入name,可以获取到value

    private static void test04() {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        //获取百度一下的value值
       String res= webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        System.out.println(res);
    }

selenium常用API的使用,测试,selenium,测试工具

selenium常用API的使用,测试,selenium,测试工具

submit:提交

使用submit搜索“软件测试”:

selenium常用API的使用,测试,selenium,测试工具

    private static void test05() {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        WebElement element=webDriver.findElement(By.cssSelector("#kw"));
        element.sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).submit();
    }

selenium常用API的使用,测试,selenium,测试工具

但是当使用submit点击超链接的按钮时: 比如新闻时

selenium常用API的使用,测试,selenium,测试工具

    private static void test06() {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).submit();
    }

结果会出现报错!!!

selenium常用API的使用,测试,selenium,测试工具

这是因为submit不支持超链接。

submit和click的区别 

可以使用submit的地方都可以使用click,但是使用click的地方不一定可以使用submit。因为click支持文本框,超链接等等,而submit则不支持超链接。 文章来源地址https://www.toymoban.com/news/detail-633244.html

到了这里,关于selenium常用API的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 自动化测试工具 —— selenium介绍及基本使用方法

    Selenium是一个开源、免费、简单、灵活,对Web浏览器支持良好的自动化测试工具,在UI自动化、爬虫等场景下是十分实用的,能够熟练掌握并使用Selenium工具可以大大的提高效率。 Selenium简介 Selenium支持多平台、多浏览器、多语言去实现自动化测试,是一个开源和可移植的Web测

    2024年02月05日
    浏览(45)
  • 测试员进阶必看系列 “ python自动化测试工具selenium使用指南 ”

    概述 python+selenium环境安装 使用selenium启动浏览器 selenium页面加载等待和检测 使用time.sleep()等待 使用implicitly_wait设置最长等待时间 使用WebDriverWait设置等待条件 检测document是否加载完成 selenium元素定位和读取 查找元素 dom元素交互 查找元素失败处理 selenium交互控制 ActionChains动

    2024年02月05日
    浏览(86)
  • Web开发自动测试工具-Selenium的具体使用办法(填坑中……)

    https://juejin.cn/post/7074779332819812389 Selenium 是最广泛使用的开源 Web UI(用户界面)自动化测试套件之一 。 Selenium 支持的语言包括C#,Java,Perl,PHP,Python 和 Ruby。目前,Selenium Web 驱动程序最受 Python 和 C#欢迎。 Selenium 测试脚本可以使用任何支持的编程语言进行编码,并且可以

    2024年02月02日
    浏览(48)
  • 简单介绍十款可以免费使用的API测试工具

    API开发应该是后端开发最常见的工作,而调试和测试API是非常关键的,这篇文章简单介绍几款常用的工具以供大家参考。 SoapUI是很老牌的工具的,在之前Webservice盛行的时候经常会用到。 现在官方推出了Pro版本的ReadyAPI,但要收费,它功能更强大,但是太贵了。 Postman是一款很

    2024年02月12日
    浏览(29)
  • 如何使用Python自动化测试工具Selenium进行网页自动化?

    Selenium 是一个流行的Web自动化测试框架, 它支持多种编程语言和浏览器,并提供了丰富的API和工具来模拟用户在浏览器中的行为 。 Selenium可以通过代码驱动浏览器自动化测试流程,包括页面导航、元素查找、数据填充、点击操作等。 与PyAutoGUI和AutoIt相比, Selenium更适合于处

    2023年04月09日
    浏览(90)
  • python自动化测试工具selenium使用指南 ,绝对能帮到你

    目录 概述 python+selenium环境安装 使用selenium启动浏览器 selenium页面加载等待和检测 使用time.sleep()等待 使用implicitly_wait设置最长等待时间 使用WebDriverWait设置等待条件 检测document是否加载完成 selenium元素定位和读取 查找元素 dom元素交互 查找元素失败处理 selenium交互控制 Actio

    2024年02月08日
    浏览(38)
  • 【APP渗透测试】Android APK常用测试工具(Drozer)安装及使用方法介绍

    本篇文章主要介绍 Drozer Android测试工具的安装以及简单使用,后续会持续更新更多使用方法。 Drozer 是 MWR Labs 开发的一款Android安全测试框架。是目前最好的Android安全测试工具之一。其官方文档说道: Drozer允许你一个普通Android应用的身份与其他应用和操作系统交互。 在Web世界

    2024年02月16日
    浏览(34)
  • 嵌入式软件开发常用的编辑代码工具、比较工具和测试工具的使用说明和操作步骤

    嵌入式软件开发常用的编辑代码工具有: Eclipse Eclipse是一款开源的集成开发环境,支持多种编程语言和插件,适用于多种嵌入式开发平台。以下是Eclipse的使用说明和操作步骤: 步骤1:创建新工程 单击“File”菜单,选择“New”→“Project”选项,选择工程类型和开发平台。

    2024年02月02日
    浏览(80)
  • OpenHarmony分布式软总线API调用测试工具 softbus_tool使用说明

    softbus_tool 是 OpenHarmony 分布式软总线 API 调用测试工具 ,文件结构如下图所示。 softbus_tool 能够将软总线 interfaces 目录下的一些常用接口集中起来,供设备间搭建一些场景时使用(比如设备绑定、BR 组网,BLE 组网, 指定 P2P 链路传输等)。 softbus_tool 一般的使用流程为: 1.使用

    2024年04月14日
    浏览(35)
  • 自动化测试工具Selenium的基本使用方法,面试字节跳动的前端工程师该怎么准备

    8.小结 上述均可以改写成find_element(By.ID,‘kw’)的形式 find_elements_by_xxx的形式是查找到多个元素,结果为列表 import time from selenium import webdriver#驱动浏览器 from selenium.webdriver import ActionChains #滑动 from selenium.webdriver.common.by import By #选择器 from selenium.webdriver.common.by import By #按照什

    2024年04月16日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包