Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python selenium and captcha

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']")

This is the xpath chrome gave me.

ERROR

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

Any ideas why this does not work?

like image 904
no nein Avatar asked Sep 19 '25 12:09

no nein


1 Answers

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']")

If you need to switch back from iframe:

driver.switch_to.default_content()
like image 91
Andersson Avatar answered Sep 22 '25 02:09

Andersson