Skip to content

Commit 9d29262

Browse files
committed
Test script for webpages that have captcha
1 parent 2ccfabc commit 9d29262

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Selenium/captcha.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This program illustrates a technique that can be used on pages that have
2+
# a captcha.
3+
# A Captha input can never by completely automated via Selenium. Usual workaround
4+
# is to sleep for some time to allow the user (usually the Tester) to enter captcha
5+
# text in input field.
6+
7+
# But in this example we are using WebDriverWait to allow the tester enough time to
8+
# manually enter the captcha text, but as soon as the captcha is entered, the test can
9+
# proceed. This is better than time.sleep()
10+
11+
from selenium import webdriver
12+
from selenium.webdriver.common.keys import Keys
13+
from selenium.webdriver.support.ui import WebDriverWait
14+
15+
driver = webdriver.Firefox()
16+
# This opens up contact trainer page on http://hadoopbigdatatutorial.com/
17+
driver.get("http://hadoopbigdatatutorial.com/home/contact_trainer/hadoop-development")
18+
19+
def check_captcha_length(driver):
20+
"""
21+
This function finds the captcha INPUT element and
22+
returns true when length of input text entered
23+
is greater than 2 characters.
24+
"""
25+
elem = driver.find_element_by_css_selector("input.captcha_input[name='userCaptcha']")
26+
return len(elem.get_attribute('value')) > 2
27+
28+
# return true if captcha input is filled with a text of length more than 2 in 30 seconds
29+
result = WebDriverWait(driver, 30).until(check_captcha_length)
30+
31+
# print title of the webpage
32+
print(driver.title)
33+
driver.close()

0 commit comments

Comments
 (0)