2

My problem is that when I switch to an iframe in python selenium, everything stops working. For example, I can call:

driver.find_element_by_xpath(XPATH HERE).click() 

before switching iframes and it works fine, but as soon as I call the code

WebDriverWait(driver,30).(EC.frame_to_be_available_and_switch_to_it)

everything stops working after the frame successfully switches.

I have switched from Chromedriver to geckodriver for firefox, and the code works without error.

I've encountered this problem ever since I downloaded the new chromedriver as my chrome browser is now version 76. I did try redownloading the chromedriver as well.

I have tried testing if there was any changes in the identifying features I used to tell the driver what to do. This includes making sure the XPATHs are still the same, the ID is still the same.

I have also tried checking if the element was located in the correct iframe or if the iframe has changed.

None of these worked.

WebDriverWait(driver,30).(EC.frame_to_be_available_and_switch_to_it).(By.ID "iframehere")))
driver.find_element_by_xpath(XPATH HERE).click()

I expected the code to run successfully, like literally a day before I had to download a new chromedriver to accomodate chrome version 76. Or how it works without errors on geckodriver.

Now, I have this error:

JavascriptException: javascript error: unhandled object (Session info: chrome=76.0.3809.100)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
wombatkingdom
  • 23
  • 1
  • 2

2 Answers2

1

When clicking on an element after <iframe> switching you have to:

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

    • Sample Code Block:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.common.by import By
      
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframehere")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "XPATH HERE"))).click()
      

Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • My code already had this. It kept timing out, so that's when I went exploring and removed the webdriver wait. I realised, even though it can switch frames and it's not giving me "cannot find element error". That's why I omitted that part from my code example. I don't know why, but ever since I updated to chrome 76, this javascript error keeps appearing even though the code worked fine before. – wombatkingdom Aug 10 '19 at 11:13
0

I had in the past problems with Selenium while using WebDriverWait() and in some cases I did overcome them simply by using

import time
time.sleep(maximum_time_needed_to_load_form)

And a very good solution but did the trick.

Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23