如何通过 webdriver.io 在 DOM 中找到具有特定 url 的链接元素

How to find a link element with an specific url inside DOM via webdriver.io

本文关键字:url 元素 链接 io webdriver 何通过 DOM      更新时间:2023-09-26

我正在使用webdriver io来测试网页。我需要检查页面上是否存在一个具有特定 href 的元素。

我已经让我尝试了这个

法典

var client = webdriverio.remote(options);
     .elements('.mylinkclass', function(err,res) {
        res.value.forEach(function(elem){
                client.getText(elem).then(function(text) {
                if (text=="www.superweb.com"){
                   console.log("allright!");
                }
            })
        })

您的代码将通过 CSS 选择器 'a' 找到一个元素,然后获取其文本。这意味着它将获得一个带有 HTML 标记 'a' 的任意元素。要解决此问题,请在 CSS 选择器中指定"href"属性的值:

client
.element("a[href='http://www.superweb.com']") // will only select a elements with the specified href attribute
.then(function(elem){
    console.log(elem); //found!
})
.catch(function(err) {
    console.log("err", err); //will be executed if no elements containing your URL are present
});

您可能还想查看链接文本选择器:

<a href="http://www.superweb.com">www.superweb.com</a>
client.getText('=www.superweb.com', function(err, text) {
    console.log(text);
});

来源: http://webdriver.io/guide/usage/selectors.html