![](/img/trans.png)
[英]How to identify and click on the Accept All button using Selenium
[英]How to click on Accept all cookies button using selenium?
我正在尝试 web 报废并在名为daft.ie
。 当我使用 selenium 打开页面时,因为它使用隐身登录它显示 cookies 每次都弹出。
我写了这段代码来点击这个按钮
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()
但这给了我以下错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"ACCEPT ALL"}
我们可以看到该按钮与 Accept All 字符串一起存在,那为什么找不到呢? 这里做错了什么?
还有无论如何通过selenium打开普通chrome,好像我想在点击cookies按钮后填写forms它需要我使用谷歌登录,所以我的Chrome登录会非常困难open 我不需要在每次运行脚本时都登录。
单击元素Accept All您需要为element_to_be_clickable()诱导WebDriverWait ,您可以使用以下任一定位器策略:
使用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()
使用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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.