Selenium 当给定xpath有多个匹配项时,如何使用xpath?
我有多个xpath匹配项:Selenium 当给定xpath有多个匹配项时,如何使用xpath?,selenium,xpath,selenium-webdriver,webdriver,Selenium,Xpath,Selenium Webdriver,Webdriver,我有多个xpath匹配项://按钮[包含(,'View Details')]。如何定位查看详细信息的第一个实例或特定实例 <div class="ma-center-button"> <button class="button button--secondary ma-center-button--block" data-ui-sref="myAccount.billing.recent-charges" href="/my-account/billing/recent
//按钮[包含(,'View Details')]
。如何定位查看详细信息的第一个实例或特定实例
<div class="ma-center-button">
<button class="button button--secondary ma-center-button--block" data-ui-sref="myAccount.billing.recent-charges" href="/my-account/billing/recent-charges">View Details</button>
</div>
查看详细信息
如果您需要第一个,只要使用,假设您使用的是Java,findElement()
:
当然还有其他方法可以找到按钮,但是,根据您提供的信息,检查href
以最近的费用结束怎么样
driver.findElement(By.cssSelector("button[href$=recent-charges]"));
将[1]附加到Xpath表达式以获取序列中的第一项。我已经给出了答案
driver.FindElement(By.XPath("//button[contains(.,'View Details')][1]")).Click();
您可以像这样使用xpath:
(//button[contains(.,'View Details')])[1]
在这里你可以通过2,3,。。而不是1 请为这个问题投赞成票,并提供完整的答案solution@E.Praneeth你能用HTML分享你的问题吗?我不知道为什么它不适合你?
(//button[contains(.,'View Details')])[1]