简体   繁体   中英

How to click on Accept all cookies button using selenium?

I am trying to web scrap and automatically fill forms in realtor website called daft.ie . When I open the page using selenium since its logs in using incognito it shows the cookies pop up everytime.

在此处输入图像描述

I wrote this code to click on this button

from selenium import webdriver
from selenium.webdriver.common.by import By

PATH = "/Users/adam/Desktop/coding/daft/chromedriver"
driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10)
link1= 'https://www.daft.ie/for-rent/griffith-wood-griffith-avenue-drumcondra-dublin-9/3523580'
driver.get(link1)
driver.find_element(By.LINK_TEXT, 'ACCEPT ALL').click()

But this gives me the following error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"ACCEPT ALL"}

We can see that button exists with Accept All string, so why cant this find it? What am doing wrong here?

Also is there anyway to open the normal chrome through selenium, as if I want to fill forms after clicking on cookies button it would require me to log in using google sign in would be very hard to do, so if I can have my normal chrome open I would not need to sign in every-time I run the script.

The click on the element Accept All 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.execute("get", {'url': 'https://www.daft.ie/for-rent/griffith-wood-griffith-avenue-drumcondra-dublin-9/3523580'}) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick*='acceptAll']"))).click()
  • Using XPATH :

     driver.execute("get", {'url': 'https://www.daft.ie/for-rent/griffith-wood-griffith-avenue-drumcondra-dublin-9/3523580'}) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='Accept All']"))).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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM