Selenium WebDriver:无法定位元素(C#)

本文关键字:元素 定位 WebDriver Selenium | 更新日期: 2023-09-27 18:00:06

我正在尝试使用C#和Selenium来自动提取Paypal。该应用程序使用提供的凭据登录Paypal,并单击"转账"链接,然后显示一个弹出窗口(看起来是iframe)。我的问题是,我无法点击弹出窗口中的任何元素,我已经尝试了我能找到的每一个建议。

以下是表单和底层html:的屏幕截图

贝宝形式

我正在尝试点击"发件人"下拉列表,以及我尝试过的其他内容:

driver.FindElement(By.XPath("//*[@id='"selection-container'"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")).Click();

((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].hidden = false;", driver.FindElement(By.XPath("//*[@id='"selection-container'"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")));

但是get和"Unable to locate element"或"element not visible"错误。如何获取弹出窗口上的"From"输入元素?(如果你正在使用贝宝,如果需要,你也可以登录并查看弹出窗口)。

Selenium WebDriver:无法定位元素(C#)

您需要首先切换到iframe

IWebElement frame = driver.FindElement(By.TagName("iframe")); // locate the iframe element
driver.SwitchTo().Frame(frame);
driver.FindElement(By.XPath("//*[@id='"selection-container'"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")).Click();

并切换回

driver.SwitchTo().DefaultContent();

尝试

[FindsBy(How = How.CssSelector, Using = "div[class$='source-dropdown']")]
public IWebElement _ddSource;

'$'指定属性的末尾,如果类的末尾是source-dropdown

首先需要切换到该iframe。使用以下代码:

IWebElement frame = driver.FindElement(By.CssSelector("iframe[src ='/moneytransfer']");
driver.SwitchTo().Frame(frame);

现在你可以使用cssSelect:点击弹出窗口

div[class$='source-dropdown']