1

I have the following HTML code from an "accept-cookie" window:

<div id="_name-buttons"> 
    <button id="_name-preferences">[...]</button> 
    <button id="_name-accept">[...]</button>

I am trying to use Selenium to click on the button _name-accept, namely accept the cookies. My code does the following:

from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

...

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, '_name-accept'))).click()

however it doesn't work and I have this error:

selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:183:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:395:5
element.find/</<@chrome://remote/content/marionette/element.js:300:16

How can I click that button id? Thank you

Frank8992
  • 47
  • 7

1 Answers1

1

To click on the element instead of id 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#_name-buttons button#_name-accept"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='_name-buttons']//button[@id='_name-accept']"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for your answer :) When I am trying your strategy I have this error: ```selenium.common.exceptions.TimeoutException: Message: Stacktrace: WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:183:5 NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:395:5 element.find/<@chrome://remote/content/marionette/element.js:300:16``` – Frank8992 Apr 26 '22 at 07:36
  • Found the issue in my approach The button was part of a iframe, so I had to select the iframe first and click the button afterwards – Frank8992 Apr 27 '22 at 20:38
  • 1
    @Frank8992 Glad to be able to help you. – undetected Selenium Apr 27 '22 at 20:40