Home > Software engineering >  Driver doesn't click with an offset all the time
Driver doesn't click with an offset all the time

Time:01-07

I wanted to set an offset to the driver's clicks so that it doesn't trigger the reCAPTCHA image recognition challenge, but this only works some of the time. Other times it would click ignore the code and click directly in the center, triggering the puzzle. Here's the code:

# system libraries
import random
import time

# selenium libraries
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains


def delay():
    time.sleep(random.randint(2, 3))


def wait():
    time.sleep(random.randint(300, 310))


def click_with_offset():
    action = ActionChains(driver)
    class_name = driver.find_element(By.CLASS_NAME, "recaptcha-checkbox-border")
    x_offset = random.randint(-11, 11)
    y_offset = random.randint(-11, 11)
    action.move_to_element_with_offset(class_name, x_offset, y_offset).click().perform()


try:
    # create edge driver
    driver = webdriver.Edge(executable_path="C:\\browserdrivers\\msedgedriver.exe")
    delay()
    # go to website
    driver.get("https://skynode.gg/apology")
    delay()

    for renew in range(7):
        # input username into field
        username = "AIM's AI"
        username_input = driver.find_element(By.ID, "username-input")
        username_input.send_keys(username)
        delay()

        # click on checkbox to activate recaptcha
        driver.switch_to.frame(0)
        print("Switched!")
        click_with_offset()
        print("Clicked!")
        delay()

        # click renew button
        driver.switch_to.default_content()
        delay()
        driver.find_element(By.ID, "submit-button").click()
        delay()
        wait()
        driver.refresh()
        delay()

except Exception as e:
    print("The error raised is", e)

I originally tried setting class_name, x_offset, and y_offset as parameters for the function and its implementation of it, but I found this to be more compact and organized. I want it to click around the box and not just directly inside, but it would only sometimes work on the first execution, but fail on the first or second loop. There are no problems with the loops, as far as I know, just the offset clicks.

Update: I increased the range of x_offset (as seen below) after discovering the ability to click outside of the checkbox border and still trigger reCAPTCHA. Now it can loop the full 7 times and there's only a small chance to hit directly at (0,0). I just need to know how to prevent it from clicking on certain pixels.

Old version:

x_offset = random.randint(-11, 11)

New version:

x_offset = random.randint(-11, 31)

CodePudding user response:

The problem that I was having was that the program would only work sometimes. This was because the chance to click directly in the center was too high. After realizing that it was possible to trigger reCAPTCHA without clicking inside the box, I adjusted the range for x_offset to a completely different area.

It used to be inside the checkbox, giving it a chance to click in the center:

x_offset = random.randint(-11, 11)

Then I increased the range so that there was a lesser chance for reCAPTCHA to detect my AI:

x_offset = random.randint(-11, 31)

Finally, I removed that chance entirely by shortening the range and increasing the minimum offset:

x_offset = random.randint(10, 31)

So now the program works smoothly through all loops and the function looks like this:

def click_with_offset():
    action = ActionChains(driver)
    class_name = driver.find_element(By.CLASS_NAME, "recaptcha-checkbox-border")
    x_offset = random.randint(10, 31)
    y_offset = random.randint(-11, 11)
    action.move_to_element_with_offset(class_name, x_offset, y_offset).click().perform()

  • Related