Niko
Niko

Reputation: 144

Selecting a Iframe with Selenium

I am trying to build a program to login to my email account based on Selenium. However I am running into the problem that a iframe Pop up will show with a button to continue and I am unable to select the button using Selenium.

The Website to login is: https://www.gmx.net/ which will switch you to this site/popup https://www.gmx.net/consent-management/

The Button on the right "Akzeptieren und weiter" ( Accept and continue ) is not selectable through Selenium.

The code I am using is:

driver.get('https://www.gmx.net')
time.sleep(5)

driver.switchTo.frame(1)

time.sleep(5)

email_text_field5 =  driver.find_element(By.XPATH, '//*[@id="save-all-pur"]')
email_text_field5.click()


time.sleep(5)

I have also tried using:

email_text_field5 =  driver.find_element(By.ID, "save-all-pur")
email_text_field5.click()

however I am continuously running into the NoSuchElementException with both methods of selection Error:

---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
Cell In [13], line 12
     10 #driver.get('https://www.gmx.net/consent-management/')
     11 time.sleep(5)
---> 12 email_text_field5 =  driver.find_element(By.ID, "save-all-pur")
     13 email_text_field5.click()
     16 time.sleep(5)

The only other method to properly select the iframe is through ID however I can't seem to find any ID for this specific Iframe

Any help would be greatly appreciated

Upvotes: 1

Views: 137

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193108

The element Akzeptieren und weiter is within nested <frame> / <iframe> elements so you have to:

  • Induce WebDriverWait for the parent frame to be available and switch to it.

  • Induce WebDriverWait for the child 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:

      driver.get('https://www.gmx.net/consent-management/')
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src='https://dl.gmx.net/permission/live/portal/v1/ppp/core.html']")))
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://plus.gmx.net/lt']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#save-all-pur"))).click()
      
    • Using XPATH:

      driver.get('https://www.gmx.net/consent-management/')
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='https://dl.gmx.net/permission/live/portal/v1/ppp/core.html']")))
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://plus.gmx.net/lt')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='save-all-pur']"))).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
    
  • Browser Snapshot:

gmx_net

Upvotes: 1

Related Questions