python - 硒 - 政策/cookies的问题
问题描述
问题:我无法点击“zgadzam sie”。发生错误“selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//span [@class ='RveJvd snByac'和text( )='Zgadzam się']"}"
from selenium import webdriver
import time
driver= webdriver.Chrome()
driver.implicitly_wait(3)
driver.get("https://www.google.com/")
driver.find_element_by_xpath("//span[@class='RveJvd snByac' and text()='Zgadzam się']").click()
driver.quit()
time.sleep(5)
解决方案
尝试单击按钮后,您正在睡觉。您还在睡觉前退出驱动程序,这是另一个问题。
考虑使用WebDriverWait().until()函数来确保加载元素而不是依赖于任意时间量:
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.presence_of_element_located((By.XPATH, "//span[@class='RveJvd snByac' and text()='Zgadzam się']"))
)
driver.find_element_by_xpath("//span[@class='RveJvd snByac' and text()='Zgadzam się']").click()
您收到此错误的原因是该元素嵌套在<iframe>
. 这可以通过等待 iframe 出现,然后等待加载按钮,最后点击按钮来解决:
# Wait for the iFrame to be available to switch to it
WebDriverWait(driver, 10).until(
EC.frame_to_be_available_and_switch_to_it(0)
)
# Wait for the button to be available within that iframe
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//span[@class='RveJvd snByac' and text()='Zgadzam się']"))
)
# Finally click the button
driver.find_element_by_xpath("//span[@class='RveJvd snByac' and text()='Zgadzam się']").click()
推荐阅读
- javascript - 为什么使用头盔后 X-Powered-By Header 未禁用?
- sql-server - 在 SQL 中将长字符串转换为 ASCII
- dll - 我收到一条错误消息,提示使用 Visual Studio 2017 重建 Pocketsphinx 时未找到 .dll
- intellij-idea - 自动导入不使用关键字import,而是粘贴全限定路径
- wordpress - 在 woocommerce 上更新订单状态时在 FB Messenger 上发送消息
- c# - Ajax 发布仅返回多个选择框的第一个值
- r - 如何对值大于的数据框进行排序
- categories - 如何访问此数组中的类别以显示在我的自定义页面上
- php - CSRF:我需要对没有 cookie 的网站使用 CSRF 令牌吗?
- go - 单个函数中的多个参数类型