0

I am trying to accept cookies consent with Selenium Python. I tried to search with CSS selector and XPath but nothing works. This is the HTML:

<button class="sc-1epc5np-0 dnGUzk sc-f7uhhq-2 coEmEP button button--filled button__acceptAll" type="button"><span theme="[object Object]" class="sc-1vlt5h-0 sc-1epc5np-1 cMLEOX baseText">Accept Cookies</span></button>
    <span theme="[object Object]" class="sc-1vlt5h-0 sc-1epc5np-1 cMLEOX baseText">Accept Cookies</span>
</button>

I tried the following code :

WebDriverWait(driver, 40).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.sc-1vlt5h-0.sc-1epc5np-1.cMLEOX.baseText')))

I also tried:

driver.find_element_by_css_selector("cMLEOX").click()
driver.find_element_by_css_selector(".cMLEOX").click()

Nothing works. What is the solution?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

The desired element is a dynamic element, so to click() on the element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button__acceptAll > span.baseText"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'button__acceptAll')]/span[text()='Accept Cookies']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352