Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 当给定xpath有多个匹配项时,如何使用xpath?_Selenium_Xpath_Selenium Webdriver_Webdriver - Fatal编程技术网

Selenium 当给定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

我有多个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-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]