如何让selenium python识别HTML中的这个按钮?
我已经尝试了这个和其他标签来获取它
driver.find_element_by_class_name('cookie-monster').clickbutton
print('button was clicked')这是按钮代码片段
<button class="cookie-monster__cta cookie-monster__cta--primary js-cookie-monster-accept">
<span class="glyphicon glyphicon-ok"></span>
Accept All Cookies
</button>发布于 2021-03-29 23:34:59
你的定位器错了。这个类被命名为cookie-monster__cta,而不是.cookie-monster。js-cookie-monster-accept似乎是唯一的类名。使用它来查找您的元素。此外,您应该等待元素,然后再单击它们。
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "js-cookie-monster-accept")))
button = driver.find_element_by_css_selector("js-cookie-monster-accept")
button.click()如果此定位器不是唯一的,请将类逐个添加到wait和find_element_by_css_selector,如下所示:
.js-cookie-monster-accept.cookie-monster__ctahttps://stackoverflow.com/questions/66855923
复制相似问题