0

Im trying to click a button and tried already several different methods with no avail.

Method 1:

WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CLASS_NAME, 'sc-gsDKAQ fWOgSr')))
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'sc-gsDKAQ fWOgSr'))))

Method 2:

driver.find_element(by=By.CLASS_NAME, value='sc-gsDKAQ fWOgSr').click()

Method 3:

btn = driver.find_element(by=By.CLASS_NAME, value='sc-gsDKAQ fWOgSr')
btn.click()

Error messages:

M1: Timeout

M2: NoSuchElementException

M3: NoSuchElementException

I tried the same by looking with Xpath, no success. Had another Error in the past, which stated that "String" cannot be interacted with.

2

1 Answer 1

1

That element is in a #shadow root element, so it's a bit fiddly to reach. Also, page seems to react at mouse movements (and only load after it detect some mouse movement). The following code seems to work: setup is on Linux, but you can adapt it to your own, just observe the imports and the part after defining the browser:

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')

This will click the button, and print in the terminal:

clicked
Sign up to request clarification or add additional context in comments.

7 Comments

I use: ´´´from webdriver_manager.chrome import ChromeDriverManager´´´ as the "traditional" way with the preinstalled driver did not work for me, to many errors to handle. Is there a way to adjust that? Basically I need a way to still use the Service without the defined path to the chromedriver
You can use whaever works on your system - as long as you have setup a functional Selenium/chromeddriver. Just observe the imports above, as well as the part after defining the browser (or driver, if you call it that).
Yea, those I have, unfortunately I get an error: ´´´TypeError: expected str, bytes or os.PathLike object, not WebDriver´´´ Im on MacOS so it should work just haven't figured out how to rewrite your code.
So you have a working setup, you can for example open the browser and navigate to google.com?
If you can access this website, and you imported all packages as above, and you respect the rest of the code, it will work. Good luck.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.