0

I am attempting to access a button element on the consent-page for mail.com.

The URL for the page can be seen here https://www.mail.com/consentpage.

If you have already been to the site, the consent-page can be viewed in a incognito tab.

Sorry I'm not sure if I should post the link or the full html here.

I am trying to access the button with the following tag

<button id="onetrust-accept-btn-handler" tabindex="0">Agree and continue</button>

link: https://ibb.co/R9bFdws

There appear to be multiple iframe posts above the button I tried to access

link: https://ibb.co/cyptbNr

My code thus far:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from time import sleep

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http:mail.com")
sleep(3)
try: 
  iframe = driver.find_elements_by_tag_name('iframe')[o]
  driver.switch_to.frame(iframe)
  button = iframe.find_element_by_id('onetrust-accept-btn-handler')
  #driver.switch_to_default_content()
  driver.quit()

except:
  print('error')
  driver.quit()

Getting access to the iframe appears to work, but after that I can not get to the button element.

I have followed the advice of other posting with this question but unfortunately cant seem to get this to work. Thanks for any help!

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
JerryTn
  • 21
  • 1

2 Answers2

0

You've goat a tricky one.

You've got iframes within iframes loaded by javascript. You need a couple of calls and you need to wait for elements to be available. You also pasted in code errors that needed to be solved.

Addressing all your issues - This works for me...

Add some more imports:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Then run this:

#updated your URL as your pasted URL wasn't valid
driver.get("https://www.mail.com/consentpage")

#Get iframe 1 - this updates DRIVER to be within that iframe
WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, ".permission-core-iframe")))

#get iframe 2 - this can only be found when driver is inside the first iframe
WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))

#Wait for the button to fully load 
button = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler")))

#click it
button.click()

Going forwards - a tip for you is to debug the code. If it's not working as you expect you need to capture and expose the full error, or more simply remove the try. Your try output just says "error" - that hides anything that actually went wrong. The actual error message it produces tells you the problem.

RichEdwards
  • 2,205
  • 2
  • 2
  • 15
0

The element with the text as Agree and continue is within multiple <iframe> elements so you have to:

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

  • Induce WebDriverWait for the desired 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.mail.com/consentpage')
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.permission-core-iframe")))
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://plus.mail.com/lt']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
      
    • Using XPATH:

      driver.get('https://www.mail.com/consentpage')
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='iframe.permission-core-iframe']")))
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://plus.mail.com/lt')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).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:

mail_dot_com


Reference

You can find a couple of relevant discussions in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217