I'm webscraping a lot of webpages. Sometimes an alert box shows. I get past these alert boxes using
browser.switch_to.alert.accept()
But after I pass a lot of pages, a captcha box shows up. As I'm passing the alert box with switch_to.alert.accept
, the code also gets past the captcha box too, when it appears (without validate it).
I am not able to stop when this captcha appears. I want to end the running of the code when the captcha appears, to prevent myself being block by the site.
How to pass the boxes and stop on captchas (and get ride of the error below)? The following algorithm works when i comment the sentence. That piece of code below is a tentative to find the captcha in the page.
print 'FIND ELEMENT?:'+browser.find_elements_by_xpath("//div[@class='phm _1yw']")
Algorithm:
for index, element in enumerate(result):
browser.get(WEBSITEa_URL+element)
try:
send= browser.find_elements_by_xpath("//div[@class='notranslate _5rpu']")[0]
send.send_keys(config.message+Keys.ENTER)
except Exception as f:
time.sleep(2)
print 'FIND ELEMENT?:'+browser.find_elements_by_xpath("//div[@class='phm _1yw']")
print 'AAA'
browser.switch_to.alert.accept()
message that shows up on prompt:
ENVIOU MENSAGEM PARA 100002557631967 5
Traceback (most recent call last):
print 'ACHOU?:'+browser.find_elements_by_xpath("//div[@class='phm _1yw']")
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 344, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 858, in find_element
'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
Message: unexpected alert open: {Alert text : }
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.3.9600 x86_64)