Python Selenium: How to let human interaction in a automated script?

Question:

I am working on a script that shows CAPTCHA and a few other stuff in a pop window. I am writing script for FireFox. Is it possible that I feed the values and on hitting Submit button script could resume the operations? I guess, some kind of infinite loop?

Asked By: Volatil3

||

Answers:

I believe what you’re asking is if it’s possible to have your selenium script pause for human interaction that can’t be fully automated.

There are several ways to do this:

Easy but hacky feeling:

In your python script, put

 import pdb 
 pdb.set_trace()

At the point you want to pause for the human. This will cause the python app to drop to a debugger. When the human has done their thing, they can type c and hit enter to continue running the selenium script.

Slightly less hacky feeling (and easier for the human).

At the point where you want to put the pause, assuming the user submits something, you can do something like (with an import time at the top):

for _ in xrange(100): # or loop forever, but this will allow it to timeout if the user falls asleep or whatever
    if driver.get_current_url.find("captcha") == -1:
        break
    time.sleep(6) # wait 6 seconds which means the user has 10 minutes before timeout occurs

More elegant approaches are left as an exercise for the reader (I know there’s a way you should be able to not have to busy-wait, but I haven’t used it in too long)

Answered By: Foon

You could wait for the submit button to be clicked by the user:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# load the page
driver.get("https://www.google.com/recaptcha/api2/demo")

# get the submit button
bt_submit = driver.find_element_by_css_selector("[type=submit]")

# wait for the user to click the submit button (check every 1s with a 1000s timeout)
WebDriverWait(driver, timeout=1000, poll_frequency=1) 
  .until(EC.staleness_of(bt_submit))

print "submitted"
Answered By: Florent B.

One way is to check for some content of the next page that loads after entering captcha and wait till they’re found.
Other way is to check for current URL until it changes (usually URL changes after entering CAPTCHA) or wait for the next URL with –

while driver.current_url == your_current_url:
    wait
Answered By: Shreemay Panhalkar

i added a breakpoint () to make the script pause for me to solve captcha manually. once done, i cant get the script to continue. when i hit the c , the url changes to reset password. how can i stay in the same url to hit login? any idea how to solve this ? thanks

Answered By: Elouali Issam
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.