1

I am trying to get account number from a web-page using requests.post

After about 5 post queries the site is requesting captcha (post answer = {"captcha_needed": true, "error": "captcha-check-failed"})

How do I simply click on the captcha box? I tried fake_useragent to avoid captcha, I also tried using selenium and chrome web driver, but it didn't help. Here is my code:

class Account(commands.Cog):

def __init__(self, bot):
    self.bot = bot
    self.servers = {'red': 1, 'green': 2, 'blue': 3, 'lime': 4}
    self.config = configparser.ConfigParser()
    self.config.read('config.ini')

@commands.command()
async def account(self, ctx, nickname):
    for allowed in eval(self.config['account']['access']):
        if ctx.message.author.id != allowed:
            flag = False
        else:
            flag = True
            break
    if flag:  
        serverName = 'red'
        server = str(serverName).lower()
        server_id = self.servers[server]
        lenght = len(nickname) + 61
        ua = UserAgent()
        userAgent = ua.random
        headers = {
                    'Accept': '*/*',
                    'Accept-Encoding': 'gzip, deflate, br',
                    'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
                    'Connection': 'keep-alive',
                    'Content-Length': str(lenght),
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                    'Cookie': '_ym_uid=155880155795497451; _ga=GA1.2.1836303506.1558801557; arp_ab=ff4d30d27222533a8a483bd63b1d7e; _ym_d=1589195755; csrftoken=QVHCsDlmUM2NSm18Xiw3QHexjMYrbbDMFITFBd4eV5hJJQJUzBjoiVQQIOEf9STZ; _gid=GA1.2.525781881.1591250700; _ym_isad=1; _ym_visorc_8171623=w; sessionid=n8b07bhdedsqt3m0swcap60avntvn6qi',
                    'Host': 'www.advance-rp.ru',
                    'Origin': 'https://www.advance-rp.ru',
                    'Referer': 'https://www.advance-rp.ru/donate/',
                    'Sec-Fetch-Dest': 'empty',
                    'Sec-Fetch-Mode': 'cors',
                    'Sec-Fetch-Site': 'same-origin',
                    'User-Agent': userAgent,
                    'X-Requested-With': 'XMLHttpRequest'
        }

        payload = {
            'g-recaptcha-response': '',
            'sum': str(random.randint(1, 1000)),
            'account': nickname,
            'service': 'unitpay',
            'server': str(server_id)
        }

        ans = requests.post('https://www.advance-rp.ru/donate/request/do/', data = payload, headers = headers)
        print(ans.text)
        if ans.text == '{"captcha_needed": true, "error": "captcha-check-failed"}':
            options = webdriver.ChromeOptions() 
            options.add_argument("start-maximized")
            options.add_experimental_option("excludeSwitches", ["enable-automation"])
            options.add_experimental_option('useAutomationExtension', False)
            driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
            driver.get('https://www.advance-rp.ru/donate/')
            WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='recaptcha-anchor']"))).click()
            ans = requests.post('https://www.advance-rp.ru/donate/request/do/', data = payload, headers = headers)
        result = ans.text.split(',')
        result.reverse()
        account_id_field = result[0]
        account_id_field = '{' + account_id_field
        account_id = eval(account_id_field)

I am creating a Discord bot so disregard all the code related to it. The main problem is that even if I solve the problem using chrome web driver, I am not sure if it going to work on a hosting service (heroku, for example). When I am trying to get this page manually, I can simply click on the captcha box and get through it, but when I tried chrome web driver, it requires additional picture selecting. I suppose using something else could do the trick. Here's the screenshot of a captcha box.

ScreenShot

I hope somebody faced similar problem before and could help me.

AMC
  • 2,642
  • 7
  • 13
  • 35
Denis
  • 21
  • 2
  • _How do I simply click on the captcha box?_ Can you not interact with it like you would any other element on the page? Related: https://stackoverflow.com/questions/58872451/how-to-bypass-google-captcha-with-selenium-and-python. – AMC Jun 05 '20 at 03:02
  • Does this answer your question? [How to bypass Google captcha with Selenium and python?](https://stackoverflow.com/questions/58872451/how-to-bypass-google-captcha-with-selenium-and-python) – Martheen Jun 05 '20 at 03:20

1 Answers1

1

Because the iframe is on a different domain you also have to launch chrome with '--disable-web-security':

opts = Options()
opts.add_argument("--disable-web-security")
driver = webdriver.Chrome(options=opts)

Then switch to the iframe to click:

iframe = driver.find_element_by_css_selector('iframe[src*="api2"]')
driver.switch_to.frame(iframe)
driver.execute_script("""
  document.querySelector('.rc-anchor-checkbox').click()
""")

You might need to adjust that css.

pguardiario
  • 53,827
  • 19
  • 119
  • 159