'How to automatically click on the "Accept cookies" button using Selenium and Python
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.
Solution 1:[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()
Solution 2:[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.
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')])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | undetected Selenium |
| Solution 2 | tehbeardedone |


