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
  • 151,581
  • 34
  • 225
  • 281
Jas_99
  • 215
  • 10

3 Answers3

2

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,758
  • 1
  • 16
  • 23
  • It doesn't work for me – Squalo Apr 13 '22 at 22:45
  • @tehbeardedone It doesn't work for me either, thanks anyway. However, I voted for your answer out of respect and esteem. Thanks for the time you spent on me and for your kindness. I appreciate :) – Jas_99 Apr 13 '22 at 23:36
  • @Jas_99 you upvoted this answer without any reasons! This answer has syntax errors also – Squalo Apr 14 '22 at 12:24
0

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
  • 151,581
  • 34
  • 225
  • 281
  • 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
-4
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
  • 79
  • 10
  • 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