Home > Blockchain >  Python selenium doesn't click on on the cookie accept button
Python selenium doesn't click on on the cookie accept button

Time:12-01

I want to access a website but I just can't get the cookie pop up to go away. The website itself opens but I can't seem to grab the accept button for the cookie. Here is my code thus far:

#set path, open firefox
path = r'C:\Users\Anwender\Downloads\geckodriver-v0.30.0-win64\geckodriver.exe'
driver = webdriver.Firefox(executable_path=path)

#open bym forum
driver.get('https://www.bym.de/forum/')

#deal with the effing cookie message
wait = WebDriverWait(driver,15)
wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[4]/div[2]/div/div/div/div/div/div/div[2]/div[2]/button[2]/span'))).click()

#log in
username = driver.find_element_by_id("navbar_username")
password = driver.find_element_by_id("navbar_password")
username.send_keys("my_username")
password.send_keys("my_password")
driver.find_element_by_xpath("/html/body/div[1]/div[2]/main/div[3]/div[1]/ul/li[2]/form/fieldset/div/div/input[4]").click()

The log in code I included to mention that I have used the exact same syntax to log into a different website so I am not sure how to get past this. The html code of the cookie button "accept" (here: "Zustimmung") is:

<button title="Zustimmen" class="message-component message-button no-children focusable button global-font-headline sp_choice_type_11 last-focusable-el" style="padding: 14px 40px 12px; margin: 12px 0px; border-width: 1px; border-color: rgb(102, 102, 102); border-radius: 20px; border-style: solid; font-size: 12px; font-weight: 700; color: rgb(17, 17, 17); font-family: georgia, serif; width: calc(100% - 80px); background: rgb(255, 255, 255) none repeat scroll 0% 0%;">Zustimmen</button>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

I have tried to grab the button by css path or by name as well but I just have no idea why it doesn't work. :/

CodePudding user response:

The accept button is present inside an iframe and you need to switch iframe first in order to access the element.

Use webdriverwait() and wait for frame to be available and switch.

//switch to iframe

WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))

//click on accept button

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[title='Zustimmen']"))).click()

You need to import below libraries.

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

CodePudding user response:

The element ZUSTIMMEN is 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:

    • Using CSS_SELECTOR:

      driver.get("https://www.bym.de/forum/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Zustimmen']"))).click()
      
    • Using XPATH:

      driver.get("https://www.bym.de/forum/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='SP Consent Message']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Zustimmen']"))).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
    

Reference

You can find a couple of relevant discussions in:

  • Related