3

I've written a script in python using selenium to tick a checkbox and hit the submit button. When I follow the steps manually, I can do it without solving any captcha. In fact, I do not face any captcha challenge. However, the site throws captchas as soon as I initiate a click on that checkbox using the script below.

website address

This is what I've tried so far:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get('https://www.truepeoplesearch.com/results?name=John%20Smithers')

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_css_selector("iframe")))
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()

How can I click on a checkbox in a webpage using selenium without triggering captchas?

SIM
  • 21,997
  • 5
  • 37
  • 109
  • can you share the screenshot,where you are selecting the checkmark. I don't see the captcha populating when tried to navigate to this page. – supputuri Mar 20 '20 at 18:02
  • When I execute the script, it triggers captchas. Here is the [screenshot](https://filebin.net/suhi6127bw0a3oap) @supputuri. However, when i visit the site manually, I do see the checkbox but I don't encounter captchas when I tick on that checkbox. Thanks. – robots.txt Mar 20 '20 at 18:10
  • Well, in that case selecting checkbox it self does not resolve your issue. You have to find a way to resolve the captcha. Btw, have you tried with your [default chrome profile](https://stackoverflow.com/questions/56344560/selenium-point-towards-default-chrome-session/56402113#56402113) to see if it's still showing the recaptcha ? – supputuri Mar 20 '20 at 18:14
  • If [this](https://filebin.net/cb65bqjvtgued21l) is what you meant, it still triggers captchas @supputuri. Thanks. – robots.txt Mar 20 '20 at 18:31

4 Answers4

3

You can use the PyMouse package (python package here) to move to the (x,y) position of the object on the webpage and simulate a mouse click.

from pymouse import PyMouse

mouse = PyMouse()
def click(self, x,y):
    """Mouse event click for webdriver"""
    global mouse
    mouse.click(x,y,1)
2

CAPTCHA is used to stop website automation & that's why it can not be automated using selenium. Adn for same reason, your not able to select CAPTCHA tick box. Please refer these link for more info: https://sqa.stackexchange.com/questions/17022/how-to-fill-captcha-using-test-automation

Kuldeep Kamune
  • 169
  • 2
  • 10
2

Here is the sample code to select the check box that will trigger the recaptcha images.

url = "https://www.google.com/recaptcha/api2/demo"
driver.get(url)

driver.switch_to.frame(driver.find_element_by_xpath("//iframe[starts-with(@name,'a-')]"))
# update the class name based on the UAT implementation (if it's different)
driver.find_element_by_class_name("recaptcha-checkbox-border").click()

But still you have to complete either image selection/use voice-to-text api to resolve the captcha. The possible options are using 3rd party APIs or check you have the APIs available in truepeoplesearch where you can get the required information as response.

Edit 1: Using the API and html parser.


url = "https://www.truepeoplesearch.com/results?name=John%20Smithers"

payload = {}
headers= {}

response = requests.request("GET", url, headers=headers, data = payload)

html_content = response.text.encode('utf8')
# now you can load this content into the lxml.html parser and get the information

html_content = response.text.encode('utf8')
root=lxml.html.document_fromstring(html_content)
content=root.xpath("//div[@class='h4']") # here I am get the names
for name in content:
    print(name.text_content() + '\n')
supputuri
  • 13,644
  • 2
  • 21
  • 39
2

If you are working on the team that develops this site, you can agree with the developers about an efficient way to work around the captcha.
For example, they could made a case in the code, captcha to not be shown if there is a cookie with hard to guess name, known only to you and them. Potentially someone can guess that cookie, but if you have no other choice, this is an option.

You can also use a separate key for testing environments as explained here.

K. B.
  • 3,342
  • 3
  • 19
  • 32