node + selenium-webdriver 进行Web自动化测试

1、环境安装

nodejs安装

webdriver安装,安装各浏览器的驱动,通过各浏览器的驱动程序,操作浏览器。

chrome 驱动安装

每个版本的浏览器用到的webdriver不同。可以通过chrome右上角-> 帮助 -> 关于 Chrome 查看当前版本。

chromedriver下载地址和chromedriver与chrome之间的对应关系。跳转门

并将驱动程序所在目录添加至环境变量

其他浏览器驱动可以见下面列表:

2、代码实现自动化测试

python 复制代码
npm init


npm install selenium-webdriver


// index.js

const {Builder, By, Key, until} = require('selenium-webdriver');

const {Options} = require('selenium-webdriver/chrome');



var driver = new Builder()

.forBrowser('chrome')

.setChromeOptions(new Options().excludeSwitches(["ignore-certificate-errors", 'user-data-dir="C:\\Users\\qiang\\AppData\\Local\\Google\\Chrome\\User Data"']))

.build();


driver.get('https://image.baidu.com/')

.then(function () {

driver.findElement(By.id('search')).then(async (res)=>{

console.log('res',await res.getText())

})

}).catch(function (e) {

console.log(e);

});

3、By XPATH和CSS定位比较

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

相关推荐
xiaofeichaichai3 小时前
Webpack
前端·webpack·node.js
问心无愧05134 小时前
ctf show web入门111
android·前端·笔记
唐某人丶4 小时前
模型越来越强,我们还需要 Agent 工程吗?—— 从价值重估到 Harness 实践
前端·agent·ai编程
007张三丰4 小时前
软件测试专栏(11/20):测试框架开发:pytest深度解析与插件体系
运维·服务器·自动化测试·pytest·测试框架
智码看视界4 小时前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
JS菌4 小时前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
excel5 小时前
HLS TS 文件损坏的元凶:Git 提交与拉取
前端
Aphasia3116 小时前
https连接传输流程
前端·面试
徐小夕6 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github