1

I'm trying to click on an accept cookies button while navigating a webpage using Selenium and Python.

Pic of cookie button

I've used inspect element to find the class name of the button, and then .click() in Python to click on it. It isn't a link, but if it were working the accept cookies pop-up would disappear, and it isn't.

HTML on website for the button

Instead, I'm getting this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".SubmitButton CookiePopup-button"}

Here is the code I am running:

cookie_button = driver.find_element("class name", "SubmitButton CookiePopup-button").click()

Thanks for your responses!

benson23
  • 16,369
  • 9
  • 19
  • 38

2 Answers2

0

The problem here is that there are spaces in the class attribute, to get around this you can instead search by using By.CSS_SELECTOR instead of "class name" and use this selector:

button[class='SubmitButton CookiePopup-button']

try and see if the below works:

cookie_button = driver.find_element(By.CSS_SELECTOR, "button[class='SubmitButton CookiePopup-button']").click()
0

You may try this, see if it works

cookie_button = driver.find_element(By.CSS_SELECTOR, "button.SubmitButton.CookiePopup-button").click()
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24