首页 文章

无法通过Selenium webdriver中的Xpath获取元素

提问于
浏览
0

HTML部分:

<li tabindex="-1" role="option" unselectable="on" class="k-item k-state-focused" data-index="0" id="6609432f-2fd2-49c1-95df-6a330acecca0">admin</li>

这是我正在尝试的Xpath:

driver.findelement(by.xpath("//li[text()=//li[. = 'admin']"));

但得到例外:

invalid selector: Unable to locate an element with the xpath expression //li[text()=//li[. = 'admin'] because of the following error:

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//li[text()=//li[. = 'admin']' is not a valid XPath expression.

准确查看UI和HTML代码:

enter image description here

该字段是自动完成的.ID正在改变该元素的动态,所以我不能在这里使用id来查找元素 .

1 回答

  • 3

    您的XPath表达式在语法上不正确,还有一个额外的 [ ,修复它:

    //li[text()='admin']
    

    或者,您可以使用 . 来引用当前节点的文本:

    //li[. = 'admin']
    

相关问题