Home > Back-end >  Python selenium cookies popup
Python selenium cookies popup

Time:08-09

I am trying to press the 'accept' button in a cookies popup in the website immobillienscout24.de

enter image description here

I understand that this requires

driver.execute_script("""return document.querySelector('#usercentrics-root')""")

But I can't trickle down the path to the 'accept' button in order to click it. Can anyone provide some help?

Thank you!

CodePudding user response:

This is one way (tested & working) you can click that button: please observe the imports, as well as the code after defining the browser/driver:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains


chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
import time as t


webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)

actions = ActionChains(browser)

url = 'https://www.immobilienscout24.at/regional/wien/wien/wohnung-kaufen'

browser.get(url) 

page_title = WebDriverWait(browser, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, "a[title='Zur Homepage']")))
actions.move_to_element(page_title).perform()
parent_div = WebDriverWait(browser, 20000).until(EC.presence_of_element_located((By.ID, "usercentrics-root"))) 
shadowRoot = browser.execute_script("return arguments[0].shadowRoot", parent_div)

try:
    button = WebDriverWait(shadowRoot, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='uc-accept-all-button']"))) 
    button.click()
    print('clicked')
except Exception as e:
    print(e)
    print('no click button')

That page is reacting to user's behavior, and it will only fully load the page once it detects mouse movements, hence the ActionChains() part of the code. After that, we drill down into the shadow root element, we locate the button (using Waits, to make sure it's clickable), and then we click it.

Selenium documentation can be found at https://www.selenium.dev/documentation/

CodePudding user response:

The element Alle akzeptieren within the website is located within a #shadow-root (open).

shadow_root


Solution

To click on the element Alle akzeptieren you have to use shadowRoot.querySelector() and you can use the following Locator Strategy:

  • Code Block:

    driver.execute("get", {'url': 'https://www.immobilienscout24.de/'})
    time.sleep(10)
    item = driver.execute_script('''return document.querySelector('div#usercentrics-root').shadowRoot.querySelector('button[data-testid="uc-accept-all-button"]')''')
    item.click()
    
  • Related