我有一个抓取机器人,我想在遇到验证码时停止,以免惹恼网站。但是selenium找不到
driver.find_element_by_xpath("//*[@id='recaptcha-anchor']")
这是 chrome 给我的 xpath。
错误
NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='recaptcha-anchor']"}
知道为什么这不起作用吗?
最佳答案
据我所知,验证码通常位于 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()
关于Python Selenium 和验证码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44187909/