在Selenium测试中使用XPath通过文本获取WebElement [英] Getting WebElement by Text using XPath in Selenium test

查看:326
本文介绍了在Selenium测试中使用XPath通过文本获取WebElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用XPath查找基于文本的任何WebElement.

I would like to find any WebElement based on text using XPath.

我有兴趣找到的WebElement,

WebElement that I am interested to find,

它的HTML

基本上,我要通过文本检索的WebElement包含一个输入元素.

Basically my WebElement that I am trying to retrieve by Text contains an input element.

我当前正在使用

driver.findElement(By.xpath("//*[normalize-space(text()) = 'Own Hotel']"));

不能在上面找到WebElement,但通常可以检索所有其他Web元素.

which does not find the WebElement above, but it usually works to retrieve all other web elements.

甚至

By.xpath("//*[contains(text(),'Own Hotel')]")

没有给我任何结果.尽管我对精确的文字匹配感兴趣.

did not give me any results. Although I am interested in exact text match.

我正在寻找一种通过文本来查找Web元素的方法,该文本与Web元素内部存在的元素无关紧要.如果文本匹配,则应返回WebElement.

I am looking for a way to find web element by text immaterial of the elements that are present inside the web element. If text matches, it should return the WebElement.

谢谢!

推荐答案

似乎文本被包装在标签内而不是输入.试试这个

It seems text is wrapped inside a label and not input. Try this

driver.findElement(By.xpath(".//label[text()[normalize-space() = 'Own Hotel']]"));

关于此xpath模式的很好的解释此处

There is nice explanation about this xpath pattern here

这篇关于在Selenium测试中使用XPath通过文本获取WebElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆