0

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)
Luiza Rodrigues
  • 185
  • 1
  • 2
  • 15
  • We need a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) – Mattwmaster58 Mar 29 '18 at 22:00
  • If the Captcha is an alert, it seems that you need to look at the content of the alert each time to see if it's got the captcha content...if it does that you stop, if not, then just accept. – Breaks Software Mar 30 '18 at 11:52
  • Hey @BreaksSoftware, that is what i am trying to do, but for some reason i can't pick up an element (find the element) before the alert appears (a normal box alert or a captcha alert). Therefore, if i can't pick up, i can't treat with if/else. – Luiza Rodrigues Aug 25 '18 at 15:17
  • to be clear, I proposed that you examine the content of the alert message to figure out if it is the captcha or a different alert: browser.switchTo().alert().getText(); – Breaks Software Aug 26 '18 at 16:09

0 Answers0