Luiz
Luiz

Reputation: 41

How can I click on a button inside an iframe using Selenium and Python

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

Upvotes: 1

Views: 5453

Answers (4)

stacktome
stacktome

Reputation: 790

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()

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193058

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

Upvotes: 3

KunduK
KunduK

Reputation: 33384

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()

Upvotes: 2

CEH
CEH

Reputation: 5909

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.

Upvotes: 0

Related Questions