user1025852
user1025852

Reputation: 2784

Test generated html by javascript, with Selenium

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?

Upvotes: 1

Views: 1470

Answers (1)

alecxe
alecxe

Reputation: 474003

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

Upvotes: 1

Related Questions