Python 硒和验证码 [英] Python selenium and captcha

查看:31
本文介绍了Python 硒和验证码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抓取机器人,当它遇到验证码时我想停止它,以免惹恼网站.但是硒找不到

I have a scraping bot which I want to stop whenever it encounters a captcha, so not to annoy the websites. But selenium can't find it

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

这是 xpath chrome 给我的.

This is the xpath chrome gave me.

错误

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

NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='recaptcha-anchor']"}

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

Any ideas why this does not work?

推荐答案

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

AFAIK, captcha usually located inside an iframe, so you can try to switch to iframe before searching for required element:

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

如果您需要从 iframe 切换回来:

If you need to switch back from iframe:

driver.switch_to.default_content()

这篇关于Python 硒和验证码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆