Aleks Lee
Aleks Lee

Reputation: 136

How to take elements by Selenium which created by JS script

I trying automating testing with Selenium (python bindings), specifically want to log in on tokmonet.zendesk.com.
I create a script which takes email field, password field and sign in button by id.
But when I ran script it fails with

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"user_email"}  

Inspecting page with Firebug I see these elements. But when trying to get them with Firefinder it couldn't. So, I perform

html_source = driver.page_source  
print(html_source)

and get the only

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>

When I check page source it contains only js scripts and no mark up.

Please advice how I could handle this elements?

Upvotes: 0

Views: 867

Answers (2)

giri-sh
giri-sh

Reputation: 6962

I see that elements that you are trying to log in are in an iframe in tokmonet.zendesk.com and so you are not able to get the elements. To handle such situation try to switch to the iframe first and then get the elements. Here's how to do it in Java -

driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
(new WebDriverWait(driver, 20))
          .until(ExpectedConditions.presenceOfElementLocated(By.id("user_email"))).sendKeys("username");
//Similarly you can get other elements too

You similarly implement it in other languages. Hope this helps.

Upvotes: 2

JeffC
JeffC

Reputation: 25731

You need to switch to the IFRAME, then send_keys() to the element which you can find by ID. Don't forget to switch back to the default content if you need to access elements outside the IFRAME.

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_id("user_email").send_keys("username")
driver.find_element_by_id("user_password").send_keys("password")
// do whatever else
driver.switch_to.default_content()

Upvotes: 1

Related Questions