0

How can I automatically click on the Facebook "Allow all cookies" button? I can't find the part of the code useful for clicking. The link is this https://www.facebook.com/privacy/consent/user_cookie_choice/?source=pft_user_cookie_choice

NOT A LINK: To view the cookie button, you must be logged in with Facebook, because there is a cookie button both before logging in and after logging in. In this case it is the cookie button after login

My code instead this:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-visualcompletion="accept_button"]'))).click()

I wrote "'button[data-visualcompletion ="accept_button"]', but the error is this.

HTML CODE enter image description here

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

3 Answers3

1

Not sure where you found that locator because that element is not a button from what I can tell. I see two options for finding this element.

enter image description here

ele = driver.find_element_by_css_selector("div[aria-label='Allow all cookies']")

# or

ele = driver.find_element_by_xpath(span[contains(text(), 'Allow all cookies')])
tehbeardedone
  • 2,833
  • 1
  • 15
  • 23
-1

To click on the Allow all cookies button you have 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, "div[aria-label='Consenti tutti i cookie'] span span"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Consenti tutti i cookie']"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I imagine this probably works but I have a question about the first option. Why the need for `span span`? If it finds the div, shouldn't it send the click to the center of that element which would indirectly click the underlying span(s)? At least that's how I understand it should work but it clearly doesn't because I used a similar approach in my answer except I left out the span elements and someone already mentioned it doesn't work. Just curious if you know why those extra two span elements make a difference. – tehbeardedone Apr 13 '22 at 22:56
  • @tehbeardedone As per [Selecting Nodes](https://i.stack.imgur.com/JAFBe.png) a space then `span` would select a multilevel descendant ``. Similarly the first span have multiple descendant and our desired element is the `` – undetected Selenium Apr 13 '22 at 23:07
  • @undetectedSelenium it doesn't work – Squalo Apr 13 '22 at 23:12
  • You should use "button" instead of span, it does the trick. (Also, you obviously have to translate the text) – Guillaume-ds May 28 '23 at 15:24
-3
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
driver.get('https://www.facebook.com/privacy/consent/user_cookie_choice/?source=pft_user_cookie_choice')
WebDriverWait(driver, 86400).until(lambda x: x.find_element_by_xpath('/html/body/div[3]/div[2]/div/div/div/div/div[3]/button[2]')).click()
Squalo
  • 119
  • 11
  • This will only work until they change the structure of the page and then it will break. It's only a temporary solution. – tehbeardedone Apr 13 '22 at 22:21