2

I am trying to write python code that clicks the "accept the cookies" button, but it doesn't work. I could not understand the problem. Can you help me? Here is my code:

Note: I am using MS Visual code, and

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://stackoverflow.com/")
driver.maximize_window()

driver.find_element(By.CLASS_NAME, "flex--item6 s-btn s-btn__primary js-accept-cookies js-consent-banner-hide").click()

My goal is automatic clicking the accept the cookies button, but the code that I run has problem, and I did not understand the problem.

The HTML of the button is:

<button 
   class="flex--item6 s-btn s-btn__primary js-accept-cookies js-consent-banner-hide">
    Accept all cookies
</button>

How can add HTML of the button? Was my way wrong?

I also added implicity wait like this: driver.implicitly_wait(15) but it doesn't click still.

Arif Esen
  • 25
  • 5
  • What is the error that being thrown? I believe it might be a synchronization issue which means you need to use some sort of implicit wait or an explicit wait. Might be also an iframe tag in the "Accept cookies" pop up. – Vladislav Bulanov Mar 06 '23 at 15:24
  • When I try with XPath, and as you said when I add implicit wait, it's works. Thanks for your comment as well – Arif Esen Mar 06 '23 at 15:49

3 Answers3

1

To click on the element Accept all cookies within the Stack Overflow homepage you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get("https://stackoverflow.com/")
    driver.maximize_window()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.js-accept-cookies"))).click()
    
  • Using XPATH:

    driver.get("https://stackoverflow.com/")
    driver.maximize_window()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Accept all 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
0

Here's a simple SeleniumBase Python solution that goes to this page and clicks the button.

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class RecorderTest(BaseCase):
    def test_recording(self):
        self.open("https://stackoverflow.com/q/75652543/7058266")
        self.click('button:contains("Accept all cookies")')
        self.sleep(3)

(Note that standard CSS doesn't have the :contains() selector. That is unique to SeleniumBase and jQuery.)

The SeleniumBase Recorder was used to generate this script in a few seconds.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
-1

I would recommend the following.

  1. Try using some different element selector such asBy.XPATH(copy as full xpath) , By.ID, By.CSS_SELECTOR

  2. Try using sleep methods as you don't want to happen the whole process so quickly.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://stackoverflow.com/")
time.sleep(1)
driver.maximize_window()
time.sleep(1)
driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/button[1]').click() #full XPATH
time.sleep(5)

I hope this would help.

Ajay S
  • 29
  • 4
  • 1
    Using sleeps are a bad practice. You should instead use `WebDriverWait`. Also, any XPath that starts from `/html` or has a lot of levels and/or multiple indices are a bad practice/very brittle. A better locator would be a CSS selector, `button.js-accept-cookies`. – JeffC Mar 06 '23 at 19:45