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定位比较

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

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

相关推荐
We་ct19 分钟前
React 性能优化精讲
前端·javascript·react.js·性能优化·前端框架·html·浏览器
云动课堂30 分钟前
【运维实战】Nginx 高性能Web服务 · 一键自动化部署方案 (适配银河麒麟 V10 / openEuler / CentOS 7/8)
运维·前端·nginx
大前端helloworld2 小时前
AI全自动实现Flutter蓝牙自动连接
前端
GISer_Jing2 小时前
从入门到落地:前端开发者的AI Agent全栈学习路线
前端·人工智能·ai编程
计算机安禾2 小时前
【Linux从入门到精通】第47篇:SystemTap与eBPF——Linux内核观测的显微镜
java·linux·前端
技术钱3 小时前
OutputParser输出解析器
linux·服务器·前端·python
可达鸭小栈4 小时前
易语言实现CSS像素文字生成器:无需字体文件渲染汉字
前端·css
xixingzhe24 小时前
Playwright 简介
自动化测试
fox_lht4 小时前
DBeaver的LightGrid 类所有函数详细分析
前端
钛态4 小时前
前端TypeScript高级技巧:让你的代码更安全
前端·vue·react·web