Home > Blockchain >  How to accept google.com cookie with selenium
How to accept google.com cookie with selenium

Time:02-05

I want to accept google.com cookie but when i put the XPATH or the name of class of button its not working. I use Firefox.

Code i have already tried :

cookie_accept_btn = driver.find_element(By.CLASS_NAME, "VfPpkd-Jh9lGc").click()

i don't know why it not work with this my error :

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .VfPpkd-Jh9lGc

I'am trying to accept the cookie of google.com

CodePudding user response:

You can find the button by searching for the text it contains

driver.find_element(By.XPATH, "//div[text()='Tout refuser']").click()

Even better, you can wait until the button becomes clickable

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
...
driver.get('https://www.google.com')
WebDriverWait(driver, 9).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Tout refuser']"))).click()

CodePudding user response:

Such web sites(amazon, google, fb etc...) put dynamic identifier for class, id etc to prevent automation. That's why it been changed frequently. In such cases, the better way to use text function of xpath.

driver.find_element(By.XPATH, "//div[text()='Tout accepter']").click()
  • Related