目录1. Selenium WebDriver 是什么?2. 安装?3. 编写脚本    3.1. 创建浏览器会话(Chrome)    3.2. 页面导航控制(加载指定测试页面)    3.3. 查找 DOM 元素    3.4. 获取元素属性    3.5. 模拟键盘动作    3.6. 模拟鼠标动作    3.7. alert 窗口控制4. 效果展示

1. Selenium WebDriver 是什么?

Selenium is an umbrella project(一揽子项目) for a range of tools and libraries that enable and support the automation of web browsers.

  • It provides extensions to emulate user interaction with browsers, a distribution server for scaling browser allocation, and the infrastructure for implementations of the W3C WebDriver specification that lets you write interchangeable code for all major web browsers.

  • At the core of Selenium is WebDriver, an interface to write instruction sets that can be run interchangeably in many browsers.

2. 安装?

Selenium Installation 分两步:

  • 选择一种测试脚本编程语言(Java、JavaScript、Ruby 等)。(这里选的是 JavaScript)

  • 安装与浏览器对应的 WebDriver 驱动包。(这里选的是 Chrome 版驱动)

    • 下载地址:https://chromedriver.storage.googleapis.com/index.html

    • 配置 WebDriver 驱动包的 PATH 变量

3. 编写脚本

3.1. 创建浏览器会话(Chrome)

const {Builder} = require('selenium-webdriver');(async function myFunction() {    let driver = await new Builder().forBrowser('chrome').build();    //your code inside this block})();

3.2. 页面导航控制(加载指定测试页面)

await driver.get('https://selenium.dev');

注:通常页面导航后,需要等到某个东西初始化完成,才能开始测试,所以需要用到 Selenium WebDriver 的 Waits 技能:

await driver.get('file:///race_condition.html');// 等待检测到变量 initialised 为 true 时,再进行后续测试await driver.wait(() => driver.executeScript('return initialised'), 10000);const element = driver.findElement(By.css('p'));assert.strictEqual(await element.getText(), 'Hello from JavaScript!');

3.3. 查找 DOM 元素

// Find Elementlet searchBar = driver.findElement(By.name('q'));// Find Elementslet elements = await driver.findElements(By.css('p'));for(let e of elements) {    console.log(await e.getText());}// Find Element From ElementWebElement searchForm = driver.findElement(By.tagName("form"));WebElement searchBox = searchForm.findElement(By.name("q"));// Find Elements From Elementlet element = driver.findElement(By.css("div"));let elements = await element.findElements(By.css("p"));for(let e of elements) {    console.log(await e.getText());}

3.4. 获取元素属性

let element = await driver.findElement(By.css("div"));const fontWeight = await element.getCssValue("font-weight"); // CSS 样式const readonly = await element.getAttribute("readonly"); // 只读属性

3.5. 模拟键盘动作

  • sendKeys

    • The sendKeys types a key sequence in DOM element even if modifier key sequence is encountered. Here are the list of possible keystrokes that WebDriver Supports.

const searchBtn = await driver.findElement(By.name('q'))await searchBtn.sendKeys('webdriver', Key.ENTER);
  • clear

    • Clears the content of an editable element. This is only applied for the elements which is editable and interactable, otherwise Selenium returns the error (invalid element state (or) Element not interactable)

3.6. 模拟鼠标动作

  • clickAndHold

let searchBtn = driver.findElement(By.linkText("Sign in"));const actions = driver.actions({async: true});// Perform mouseMove to element and mouseDown (press) action on the elementawait actions.move({origin:searchBtn}).press().perform();
  • contextClick

let searchBtn = driver.findElement(By.linkText("Sign in"));const actions = driver.actions({async: true});// Perform context-click action on the elementawait actions.contextClick(searchBtn).perform();
  • doubleClick

let searchBtn = driver.findElement(By.linkText("Sign in"));const actions = driver.actions({async: true});// Perform double-click action on the elementawait actions.doubleClick(searchBtn).perform();
  • moveToElement

let gmailLink = driver.findElement(By.linkText("Gmail"));const actions = driver.actions({async: true});// Performs mouse move action onto the elementawait actions.move({origin:gmailLink}).perform();
  • release

// Store 'box A' as source elementlet sourceEle = driver.findElement(By.id("draggable"));// Store 'box B' as source elementlet targetEle = driver.findElement(By.id("droppable"));const actions = driver.actions({async: true});await actions.move({origin:sourceEle}).press().perform();// Performs release event on target elementawait actions.move({origin:targetEle}).release().perform();

3.7. alert 窗口控制

//Click the link to activate the alertawait driver.findElement(By.linkText('See an example alert')).click();// Wait for the alert to be displayedawait driver.wait(until.alertIsPresent());// Store the alert in a variablelet alert = await driver.switchTo().alert();//Store the alert text in a variablelet alertText = await alert.getText();//Press the OK buttonawait alert.accept();

4. 效果展示

参考:

Selenium WebDriver 下载页:

https://www.selenium.dev/documentation/en/webdriver/driver_requirements/

Chrome 版 WebDriver 下载地址:

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

Selenium WebDriver -> Waits:

https://www.selenium.dev/documentation/en/webdriver/waits/


自动化测试 div sendkeys无效_【自动化测试】【JestSelenium】(04)—— Selenium WebDriver...相关推荐

  1. python自动化测试脚本怎么写_自动化测试脚本一般用什么语言写

    1. 自动化测试脚本一般用什么语言写 我主要是做web测试的,最近也在研究自动化测试. 自动化测试主流的工具应该是QTP吧,这个你可以去51testing论坛搜到很多相关的资料.QTP是一个商业软件. ...

  2. 11小时 python自动化测试从入门到_自动化测试实战宝典(Robot Framework+Python从小工到专家)...

    目录 第1部分 入门篇 第1章 大话测试行业 1.1 测试行业现状分析 1.2 未来发展趋势 1.3 测试工程师的核心竞争力 1.4 测试知识体系 1.5 测试发展能力图谱及推荐书单 1.6 本章总结 ...

  3. python webdriver点击指令_测开系列Selenium Webdriver Python(20)--Webdriver运行原理

    Webdriver运行原理 转帖请注明出处!谢谢 在开发Webdriver的自动化脚本过程中,Webdriver后台在创建WebDriver实例的过程中,先确认浏览器的原生组件中是否存在可匹配的版本. ...

  4. uft自动化测试工具安装步骤_自动化测试灵魂三问:是什么、为什么和做什么

    什么是自动化测试 在软件测试领域,有两种测试技术:「手动测试」和「自动测试」.两种方法都旨在执行测试用例,然后将实际结果与预期结果进行比较. 概括来讲手动测试是一种人工执行操作的测试技术,可确保软件代 ...

  5. 自动化测试python 成长路线_自动化测试工程师成长路线规划

    随着互联网产品功能的迭代更新,对软件质量和数据安全也逐渐提出了更高的要求.因此,测试在目前的地位和功能可以说是越来越不可或缺了.虽然目前的就业市场继续大量的测试人才,但是低端的测试人员还是比较饱和的. ...

  6. java web类名_带有Java的Selenium Webdriver:使用一个命令定位具有多个类名的元素

    我正在尝试使用Selenium(2.31.0,使用JavaSE 1.6和IE9)在页面上查找一系列元素.这些元素都有两个类名称之一,'dataLabel'或'dataLabelWide'.目前,我的代 ...

  7. python调用chrome插件_使用Python通过Selenium WebDriver打开chrome扩展

    我们有类似的要求,使用Selenium WebDriver处理chrome附加组件.正如'@Aleksandar Popovic'所说,我们无法使用WebDriver点击chrome扩展图标,因为图标 ...

  8. python写ui自动化测试用例_自动化测试(6) | Web UI 自动化测试方案

    Web项目的 UI 自动化测试方案 有用的链接: 项目讨论 项目中符合自动化测试的部分有哪些?(目标和范围 scope, 准入准出标准) 稳定的需求点.变动较少的页面 每日构建后的测试验证 daily ...

  9. airtest自动化测试_自动化测试必备实用工具,帮你提高工作效率|Gitee项目推荐...

    今天,小编从Web UI 测试.API 测试.数据库测试.接口测试.单元测试.性能测试等方面,为大家整理了 Gitee 上优秀的自动化测试开源项目,希望能给大家带来一点帮助.不足之处,欢迎讨论交流:) ...

最新文章

  1. python适合做后端开发吗-pythonWeb后端开发好呢?还是从事网络爬虫比较好呢?
  2. 困了。还得背课文。变词型
  3. 2018年python薪资_2018年国内就业薪资高的7大编程语言排行
  4. Bootstrap 幻灯片效果
  5. 混沌图像---马丁迭代【密集恐惧症患者慎入】
  6. A simple BBS demo including(CRUD) - 1
  7. Xshell远程连接Linux失败
  8. 华为笔试题 2022.3.30
  9. Android-jsoup爬虫
  10. protel99se进阶视频教程(手把手教你画51单片机开发板)
  11. laravel文档——文件夹结构
  12. css动画效果-animation
  13. 南京美食指南(完美篇)
  14. 火山PC(火山视窗)超级列表框使用案例教程
  15. 某小说App返回数据 解密分析
  16. FairyGUI个人使用手册 只有重点(1)
  17. 遍历当前目录删除指定的文件夹——批处理
  18. 初识网络原理——网络原理的基本概念
  19. 安卓基于MDNS协议的局域网内服务发现
  20. dhu复试基础——60 发牌

热门文章

  1. SpringCloud教程- 服务消费者(Feign)(SpringCloud版本Finchley)
  2. SpringBatch 配置并行启动Job详解 (八)
  3. linux find批量替换java文件中字符串
  4. K8S部署工具:KubeOperator集群部署
  5. 【收藏】OGC标准服务 WMS WCS WFS WPS
  6. Linux shell删除变量
  7. Java工具类--雪花算法生成全局唯一ID
  8. idea安装行号快速定位行快捷键以及设置方法
  9. std中稳定排序算法_敏哥:深挖亚马逊A9算法中的广告搜索排序
  10. Windows Powershell的一些常规操作命令