I have a little trouble to find a button on the hotel comparators prices named trivago.es.
I want to accept the cookies one time before I do my tests but I have problems to identify (by any way (css_Selector
, id
, className
...) the button for accepting/decline cookies.
I try by looking simply on the class name and telling the selenium driver to wait until the button will be visible
driver=webdriver.Firefox()
driver.get('https://www.trivago.es/')
wait=WebDriverWait(driver,10)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"button.sc-dcJsrY:nth-child(3)")))
but an exception occurs:
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:193:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:511:5
dom.find/</<@chrome://remote/content/shared/DOM.sys.mjs:136:16
I try this way too a bit more specific but none too:
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"button[data-testid='uc-accept-all-button][role='button']")))
#Or simply by the classname but same error:
driver.find_element(By.CLASS_NAME,"sc-dcJsrY fGREIx")
knowing that in the html we have:
<button role="button" data-testid="uc-accept-all-button" style="font-size: 0.813em; font-weight: 400; height: 43px; margin: 0px 6px;" class="sc-dcJsrY fudOQx">Allow all</button>
The targeted element(Allow all) is within a shadow-root. In such cases, you need to use JS
to access elements inside it.
Check the working code below:
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.trivago.es/")
driver.maximize_window()
# wait = WebDriverWait(driver, 10)
time.sleep(5)
# Access the element inside shadow root using JavaScript and click it
cookie_btn = driver.execute_script("""return document.querySelector('div#usercentrics-root').shadowRoot.querySelector('button.sc-dcJsrY.fudOQx')""")
cookie_btn.click()
time.sleep(10)
Similar issue for your reference - https://stackoverflow.com/a/77989300/7598774