'How to locate an element inside this iframe with selenium?

I am tring to access an element inside this iframe:

I am tring to access an element inside this iframe

I tried to use switch_to.frame(0) first, but still can not locate the element inside the frame.

Screenshot of the error:

enter image description here



Solution 1:[1]

As the element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='To Do Assignments in Connect'][src='https://connect.mheducation.com/paamweb/index.html#/access/home']")))
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='To Do Assignments in Connect' and @src='https://connect.mheducation.com/paamweb/index.html#/access/home']")))
      
  • Note : You have to add the following imports :

     from selenium.webdriver.support.ui import WebDriverWait
     from selenium.webdriver.common.by import By
     from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant detailed discussions in:

Solution 2:[2]

You should switch to the frame:

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 TeRe