1

I'm trying to access some elements inside of iframe but withour success. Basically, I found the iframe, switch to it, but I can get the element inside of it. All elements that I try, I always get a message saying that the element was not found.

Could you please help?

HTML:

HTML

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
Peterwedge
  • 23
  • 3

2 Answers2

0

You can use the below code to switch to frame and then interact with the desired element :-

wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(@sandbox,'allow-popups-to-escape-sandbox')]")))
ele = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.analytics-ui-application")))
#ele is a web element so you can trigger .click, .text or any other web element methods on it. 

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

As the elements are within an <iframe> so 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 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,"frame-router[activeclient='analyticsUi'] > iframe[sandbox]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "css_clickable_element"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame-router[@activeclient='analyticsUi']/iframe[@sandbox]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "xpath_clickable_element"))).click()
      
  • 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
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352