【问题标题】:Python selenium and captchaPython 硒和验证码
【发布时间】:2017-05-25 18:57:12
【问题描述】:

我有一个抓取机器人,只要它遇到验证码,我就想停止它,以免惹恼网站。但是selenium找不到

driver.find_element_by_xpath("//*[@id='recaptcha-anchor']")

这是 xpath chrome 给我的。

错误

NoSuchElementException: 无法定位元素:{"method":"xpath","selector":"//*[@id='recaptcha-anchor']"}

任何想法为什么这不起作用?

【问题讨论】:

  • Chrome 给定的 xpath 可能非常通用,您可能需要查看 html 并实际了解 xpath 的基本语法,并更好地使其更具体,抓住您真正想要的内容
  • 好的,你有什么链接可以让我了解更多相关信息吗?
  • 顺便说一句,我刚刚注意到你的xpath 已经非常具体了,因为它有@id=...,那么为什么它不起作用呢?
  • 只是给了我以下错误:NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='recaptcha-anchor']" }

标签: python selenium xpath


【解决方案1】:

AFAIK,验证码通常位于iframe 内,因此您可以在搜索所需元素之前尝试切换到iframe

frame = driver.find_element_by_xpath('//iframe[contains(@src, "recaptcha")]')
driver.switch_to.frame(frame)
driver.find_element_by_xpath("//*[@id='recaptcha-anchor']")

如果需要从iframe切换回来:

driver.switch_to.default_content()

【讨论】:

  • 你真是个该死的天才!
  • 一个额外的小问题,我如何更改框架?
  • 你是什么意思frameback?你的意思是你怎么能从iframe切换回来?检查更新的答案
猜你喜欢
  • 2019-06-17
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
相关资源
最近更新 更多