javascript selenium判断一个元素存在的方法(CukeTest环境)

Posted 测试小小蘇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript selenium判断一个元素存在的方法(CukeTest环境)相关的知识,希望对你有一定的参考价值。

1.在【features】-【step_definitions】目录下创建自己的一个js文件,内容如下

 

var { Given, When, Then } = require(‘cucumber‘)
const { driver } = require(‘../support/web_driver‘);
const {By,until,Key} = require(‘selenium-webdriver‘);

async function isExist(ele) {
var bool=null
await driver.findElement(By.xpath(ele)).then(function(result){
// 获取成功时的处理
console.log("元素找到")
bool = true
}).catch(function(error){
// 获取失败时的处理
console.log("元素未找到")
bool = false
});
return bool
};

module.exports = {
isExist
}
 
2.在definitions.js下引用自定义的js文件
var user = require(‘./user‘)
 
然后调用
let a = await user.isExist(‘//*[@id="app"]/div/div/ul/li[2]/a‘)
 
这样通过判断a的值,就可以实现判断一个元素在页面中是否存在
 
 

以上是关于javascript selenium判断一个元素存在的方法(CukeTest环境)的主要内容,如果未能解决你的问题,请参考以下文章

java中selenium判断某个元素是否存在

selenium环境搭建及基本元素定位方式

WebDriver(Selenium2)判断元素是不是存在。

python selenium判断元素是否存在的问题

python selenium判断元素是否存在的问题

Python Selenium.WebDriverWait 判断元素是否存在