Home > Blockchain >  How to locate an element inside this iframe with selenium?
How to locate an element inside this iframe with selenium?

Time:04-04

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

CodePudding user response:

You should switch to the frame:

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

CodePudding user response:

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:

  • Related