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?
3 Answers
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"
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)