1

I'm trying to click on a button "Administration" inside an iframe but I'm getting this error:

selenium.common.exceptions.TimeoutException: Message:

Python code I am using:

main = driver.find_element_by_xpath("//div[@class='main absolute']")
main.click()
driver.switch_to.frame("tab_Welcome")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.wah-global-ask-banner-item div.wah-global-ask-banner-item-title.wah-global-ask-banner-item-title-paa")))
button.click()

HTML:

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Luiz
  • 41
  • 1
  • 8
  • Welcome to Stack Overflow! Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. – JeffC Sep 25 '19 at 14:59

4 Answers4

3

To click() on the element with text as Administration as the the desired 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:

    • CSS_SELECTOR:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.iframe-content#tab_Welcome")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.wah-global-ask-banner-item-title.wah-global-ask-banner-item-title-paa"))).click()
      
    • XPATH:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='iframe-content' and @id='tab_Welcome']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='wah-global-ask-banner-item-title wah-global-ask-banner-item-title-paa' and text()='Administration']"))).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
      

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

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

Induce WebDriverWait and frame_to_be_available_and_switch_to_it() Induce WebDriverWait and element_to_be_clickable() and Following XPATH.

main = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='main absolute']")))
main.click()
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"frame_Welcome")))
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Administration']")))
button.click()
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

It looks like you're switching to the iframe using its ID, but you need to switch to it by name.

So instead of driver.switch_to.frame("tab_Welcome")

You should try driver.switch_to.frame("frame_Welcome")

Hope this helps.

CEH
  • 5,701
  • 2
  • 16
  • 40
  • There is an existing `ExpectedCondition` that handles waiting for a frame, `EC.frame_to_be_available_and_switch_to_it()`. See KunduK's answer. – JeffC Sep 25 '19 at 15:01
  • @Luiz It's possible the issue is with the CSS selector itself which is causing the timeout, even if you successfully switched to the iframe. It looks like @KunduK has implemented a correct XPath selector that you can use: `//div[text()='Administration']` – CEH Sep 25 '19 at 15:03
0
def find_all_iframes(driver):
    iframes = driver.find_elements_by_xpath("//iframe")
    for index, iframe in enumerate(iframes):
        # Your sweet business logic applied to iframe goes here.
        driver.switch_to.frame(index)
        find_all_iframes(driver)
        driver.switch_to.parent_frame()
stacktome
  • 790
  • 2
  • 9
  • 32