Html Selenium:通过可见文本查找元素

Html Selenium:通过可见文本查找元素,html,selenium,xpath,Html,Selenium,Xpath,在html页面中,我有以下应答器: <button class="....." data-toggle="dropdown"> Text: Title of the button <span class="....."/> </button> 但是它不起作用。 我怎样才能检测到这个按钮 仅供参考:我无法使用类检测到它,因为有多个与该类匹配的节点。您好,请按以下方式执行 By.xpath("//button[co

在html页面中,我有以下应答器:

<button class="....." data-toggle="dropdown">
Text: Title of the button                        
<span class="....."/>
</button>
但是它不起作用。 我怎样才能检测到这个按钮


仅供参考:我无法使用类检测到它,因为有多个与该类匹配的节点。

您好,请按以下方式执行

By.xpath("//button[contains(text(),'Text: Title of the button')]")
please put Text: Title of the button in single quote.
Also if more html source code is provided then i can come up with a better locator stratgey
您也可以根据类名或属性来执行此操作 data toggle=xpath中的下拉列表,与/*[@data toggle='dropdown']类似

//获取列表中按钮的类名

List<WebElement> buttons= driver.findElements(By.className("className"));
Now you can select your concerned button on the basis of index (in java index starts 
form zero)
containstext“…”仅计算当前上下文元素中的第一个文本节点子节点。对于您所看到的示例元素,这应该不会有任何问题,但它不适用于以下元素(例如),因为按钮内的第一个文本节点是位于span之前的换行符:


上面的XPath测试单个文本节点是否包含某些文本。

请尝试/*[containstext,text:Title of The button][0]或与我共享代码。请查看该文本是否位于按钮标记内。如果是,请检查文本的部分//按钮[containstext,'part of Title of the button']@IAH2iV您可以看到上面HTML的代码,此文本位于按钮标记内,但问题是按钮内包含其他应答器。@Kichan Patel,问题不在于XPATH有更多匹配的节点,而在于XPATH与任何节点都不匹配。事实上,我的问题是除了Containstext之外,是否还有其他方法通过内部文本检测元素,因为在我的例子中,所有其他的解决方案都不是更好的方法。有12个以上的类匹配节点和10个以上的数据切换匹配节点,我有5个以上的按钮,我想用基于可见文本的通用解决方案来检测它们。ok plz confirm By.xpath//button[containstext,'text:Title of the button']这行行行不行不行这行不行我以前试过!。我有一些在元素中没有应答器的例子,只有文本和它工作,但是在我的cas中没有。你能发布你的问题区域的完整源代码吗对不起,源代码是机密的,我不能发布。谢谢你的帮助。
List<WebElement> buttons= driver.findElements(By.className("className"));
Now you can select your concerned button on the basis of index (in java index starts 
form zero)
<button class="....." data-toggle="dropdown">
<span class="....."/>
Text: Title of the button                        
</button>
.//*[text()[contains(.,"Text: Title of the button")]]