简体   繁体   中英

Selenium WebDriver: unable to locate element (C#)

I'm trying to automate Paypal withdrawals by using C# and Selenium. The application logs into Paypal using provided credentials and clicks on the 'transfer money' link, which then shows a pop-up (which looks to be an iframe). My problem is that I can't click on any of the elements in the pop-up, and I've tried every suggestion I could find.

Here is a screenshot of the form and the underlying html:

paypal form

I'm trying to click on the 'From' dropdown and among other things I've tried:

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

and

((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]")));

but either get and 'Unable to locate element' or 'Element not visible' errors. How do I get to the 'From' input element on the pop-up? (If you're using paypal, you could also log in and take a peek at the pop-up if needed).

You need to switch to the iframe first

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();

And to switch back

driver.SwitchTo().DefaultContent();

Try

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

The '$' specifies the end of the attribute, in the case the end of the class is source-dropdown

at first you need to switch to that iframe. use the below code:

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

now you can click on that pop up by using this cssSelector:

div[class$='source-dropdown']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM