Selenium WebDriver with c# -我如何定位这些按钮

本文关键字:定位 按钮 何定位 with WebDriver Selenium | 更新日期: 2023-09-27 18:19:03

我试图点击这些按钮,但我没有得到任何东西。就连xpath也无法识别它们。有人知道怎么定位这些按钮吗?

<button class="rt-button rt-dialog-button" title="" value="">Close</button>
<button class="rt-button rt-dialog-button" title="" value="">OK</button>

非常感谢您的帮助

编辑:

谢谢你的帮助。没有一个建议对我有用,但我还是用:

c#

IWebElement OKButton = driver.FindElement(By.XPath(("//button[@class='rt-button rt-dialog-button'][2]")));
IWebElement CloseButton = driver.FindElement(By.XPath(("//button[@class='rt-button rt-dialog-button'][1]")));

Selenium WebDriver with c# -我如何定位这些按钮

我会使用xpath找到它们:

By.XPath("//button[text()='Close']")
By.XPath("//button[text()='OK']")

您可以为每个元素添加一个ID。

<button id="closeButton" class="rt-button rt-dialog-button" title="" value="">Close</button>
<button id="okButton" class="rt-button rt-dialog-button" title="" value="">OK</button>

然后你可以像

这样找到它
IWebElement element = driver.FindElement(By.Id("closeButton"));
IWebElement element = driver.FindElement(By.Id("openButton"));

我曾经有几次很难通过类和x-path和ID进行捕获。

我想你可以试试这个xpath:

//button[contains(text(),'Close')]

//[.='OK']

如果这不起作用,那么你有多个元素隐藏了相同的文本或在页面的某个地方