Clicking the button with Selenium Python

Question:

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.

Asked By: Arif Esen

||

Answers:

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.

Answered By: Michael Mintz

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.

Answered By: Ajay S

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
    
Answered By: undetected Selenium