1

New at Selenium (C#). Wanted to automate some third party page login. When I navigate manually and in chrome F12 > "View Element", I see the text boxes good.

<input type="text" id="username" name="username" >
<input type="password" id="password" name="password" >

However, when I do "View Source" I don't see that. I assume there is Javascript code that generates this login form.

In Selenium - it works on the "View Source" version of course - when I do the following I get - "No Such Element" as expected...

var x = Driver.FindElement(By.Name("username"));

Is it possible for Selenium to interacts with fields that were generated dynamically like in my case? Like tell it to "wait" or dive to the dynamic version of the html or something?

user1025852
  • 2,684
  • 11
  • 36
  • 58
  • 1
    Are you sure there are no iframes on the page? – alecxe Jan 12 '15 at 20:47
  • not so familiar with IFrame, but you're right there are IFrames...don't see a place I can get a hold and fill the fields in these iframes... – user1025852 Jan 12 '15 at 20:51
  • ok you are absoulutly right - this is IFrame so I went to the IFrame page and now I'm able to interact with the fields. many thanks!! – user1025852 Jan 12 '15 at 20:55

1 Answers1

1

In case the target element is not inside an iframe, then using an Explicit Wait should solve the problem:

IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("username")));

See also: Selenium c# Webdriver: Wait Until Element is Present

If the element is inside an iframe, you should first switch to it:

IWebElement frame = driver.FindElement(By.Id("my_frame_id"));
driver.SwitchTo().Frame(frame);

See also: Finding nested iFrame using Selenium 2

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • this is so cool - is it fair to say that if selenium waits - its source will be updated with all the generated content? – user1025852 Jan 12 '15 at 20:56
  • @user1025852 you can say so, in this case the webdriver would periodically ask for the element (polling) until a timeout (3 seconds in the example). As far as I recall, the default polling interval is 500 milliseconds. – alecxe Jan 12 '15 at 20:57