1

Here's a simple example:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.dcor.state.ga.us/GDC/Offender/Query")
button = driver.find_element_by_id('submit2')

The last command fails no matter how long I wait (unlike previous questions and answers-a long time after the page loads), but when I'm going to the browser itself(the instance that Selenium created), the green "I agree" button has the id submit2(right click->Inspect element)...

What causes this behavior?

How can I make it work?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Yoni Keren
  • 1,170
  • 2
  • 13
  • 24
  • The button is inside ` – Guy Oct 08 '18 at 11:42

2 Answers2

1

That's because form located inside an iframe, so you need to switch to that frame before locating element:

driver.switch_to.frame('iframe-content')
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Thanks man! Where can I learn more about simulating user's actions? For example, Now I found out that (on the same webpage) calling the "click" method doesn't actually work for some reason-unless I manually scroll down on the browser itself – Yoni Keren Oct 08 '18 at 12:06
  • Check [how to perform scrolling to desired element](https://stackoverflow.com/questions/41744368/scrolling-to-element-using-webdriver) – Andersson Oct 08 '18 at 12:07
  • I found 2 quetions about scrolling in Selenium and neither method worked... Here's the code: driver = webdriver.Firefox() driver.get("http://www.dcor.state.ga.us/GDC/Offender/Query") driver.switch_to.frame('iframe-content') #driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") button=driver.find_element_by_id('submit2') actions = ActionChains(driver) actions.move_to_element(button).perform() button.click() – Yoni Keren Oct 08 '18 at 12:16
  • Do you want to scroll to that Submit2 button? – Andersson Oct 08 '18 at 12:17
  • Probably- I just want to get through to the next page- I'm not 100% sure why it doesn't work without scrolling. – Yoni Keren Oct 08 '18 at 12:19
  • Did you try to wait until button becomes clickable? Which exception is triggered when you're trying to click button? – Andersson Oct 08 '18 at 12:21
  • No exceptions raised it just didn't go through to the next page. In the end driver.execute_script("arguments[0].scrollIntoView();", button) worked-but 2 other "scrolling" method didn't do anything. Anyways, since it seems magical to me (no idea why the 2 other scrolling methods didnt work etc), are you familiar with a proper tutorial about Selenium? – Yoni Keren Oct 08 '18 at 12:24
  • 1
    Each approach might be applicable to specific case and might not work in another case. [Documentation](https://selenium-python.readthedocs.io/index.html) and StackOverflow is all you need to be Selenium guru :) – Andersson Oct 08 '18 at 12:38
0

The desired element is within an <iframe> so you need to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable and you can use the following solution:
  • Code Block:

    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe-content")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.oq-sub.btn.btn-success#submit2"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Why OP need *Induce WebDriverWait for the desired frame to be available and switch to it*? Any reason to *wait for static node*? – Andersson Oct 08 '18 at 12:00