Switch to an Iframe Through Selenium and Python

How to change between iframes in Selenium and Python

The website is Power BI based, so to switch within the <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='PowerBi Report Viewer']")))
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='PowerBi Report Viewer']")))
  • 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:

  • Switch to an iframe through Selenium and python
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
  • selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Select iframe using Python + Selenium

What finally worked for me was:

        sel.run_script("$('#upload_file_frame').contents().find('img[alt=\"Humana\"]').click();")

Basically, don't use selenium to find the link in the iframe and click on it; use jQuery. Selenium has the capability to run an arbitrary piece of javascript apparently (this is python-selenium, I am guessing the original selenium command is runScript or something), and once I can use jQuery I can do something like this: Selecting a form which is in an iframe using jQuery

How to switch between iframes using Selenium and Python?

The card number and name fields are in different <iframe> so you have to:

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

  • Switch to the Default Content

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

  • You can use either of the following xpath based Locator Strategies:

    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='card-fields-iframe' and starts-with(@id, 'card-fields-number')]")))
    # perform other operations
    driver.switch_to.default_content()
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='card-fields-iframe' and starts-with(@id, 'card-fields-name')]")))
  • 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:

  • Ways to deal with #document under iframe
  • Switch to an iframe through Selenium and python

How can I switch to the iframe using Selenium and python?

The element with the text as E-Mail Login 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.zoid-visible[title='ec_payment'][name^='__zoid__ec__payment']")))
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='zoid-visible' and @title='ec_payment'][starts-with(@name, '__zoid__ec__payment')]")))
  • 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:

  • Ways to deal with #document under iframe
  • Switch to an iframe through Selenium and python
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
  • selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

How do I Switch to Iframe and enter text in field with Selenium

The iframe has a class-name - webAuthContainer__iframe. Can make use of the same to switch to the iframe.

please = driver.find_element_by_css_selector('button.frontHero__loginButton')
please.click()

# Switch to iframe using class-name
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME,"webAuthContainer__iframe")))

attempt = wait.until(EC.element_to_be_clickable((By.ID,"sign_in_up_email")))
attempt.send_keys("example@email.com")

# Switch to default content to interact with elements outside the iframe
driver.switch_to.default_content()

switch iframe selenium Python without ID

You could use xpath to find the iframe, so something like "//iframe[@class='credit-card-iframe-cvv mt1 u-full-width']" as your find_by_xpath condition.

my_iframe=driver.find_element_by_xpath("//iframe[@class='credit-card-iframe-cvv mt1 u-full-width']")
driver.switch_to.frame(my_iframe)

A more robust version of the above would be:

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

my_iframe=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//iframe[@class='credit-card-iframe-cvv mt1 u-full-width']")))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(my_iframe))

Switch back to default content like so:

driver.switch_to.default_content()


Related Topics



Leave a reply



Submit