3

I am trying to fill forms on certain webpages. I am having success with most pages however certain ones are giving me trouble.

Some pages have a format where the form is not initially visible on the page itself, instead you need to click a button which then opens a (what seems to be JavaScript) pop up within the page which then needs to be filled.

I have looked around and have seen examples which address how to fill forms which get created in a new pop up window and which get created in alert windows. However I can not seem to find any examples which can solve this particular case.

This is the code I have so far.

     driver = webdriver.Chrome()
     driver.implicitly_wait(10)

     mouse = webdriver.ActionChains(driver)    
     driver.get(url)

     driver.maximize_window()

     value = 'fill form'
     span_xpath = '//span[contains(text(), "' + value + '")]'
     span_element = driver.find_element_by_xpath(span_xpath)
     mouse.move_to_element(span_element).click().perform()
     time.sleep(5)

Everything works till here, the fill form button gets clicked which opens the form in the browser page.

But when the following is execute an error always arises: "no such element: unable to locate element"

     n =driver.find_element_by_id('name')
     n.send_keys('john smith')

I have tried to locate the element by, name, id, xpath and so on but no matter what try, it can not locate any of the form elements.

I would greatly appreciate any help on this matter.

6
  • Make sure pop element is not inside any frame/iframe?? Commented Nov 15, 2016 at 0:27
  • @SaurabhGaur, Yes it is in a iframe. However its an iframe within an iframe. I can access the src of the first iframe using src = driver.find_element_by_id("iframe_id").get_attribute("src") . But the second iframe withiin that iframe does not have an id so i am not sure how to get its src Commented Nov 15, 2016 at 0:48
  • You need to switch first parent iframe before accessing child iframe Commented Nov 15, 2016 at 2:59
  • @SaurabhGaur I tried to do: driver.switch_to_frame(driver.find_element_by_name("iframe_name")) and driver.find_element_by_xpath('/html/body/iframe') but i still get the same element not found error Commented Nov 15, 2016 at 3:06
  • @SaurabhGaur This link describes the issue i am having: bugs.chromium.org/p/chromedriver/issues/detail?id=575 i can switch into the first iframe by doing driver.switch_to_frame(0) but once i switch into the nested iframe by doing driver.switch_to_frame(0) again i get an error Commented Nov 15, 2016 at 3:28

1 Answer 1

1

First switch using driver.switch_to_frame(0) (parent iFrame) then switch using driver.switch_to_frame(1) (chiled iFrame) and then find the element.

Note: instead of using index you can identify the frame using id, name, xpath etc. like any other Web Element and pass that element to switch_to_frame as an argument. This gives more consistency.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.