简体   繁体   English

如何使用请求和 selenium 单击验证码框?

[英]How do I click captcha box using requests and selenium?

I am trying to get account number from a web-page using requests.post我正在尝试使用 requests.post 从网页获取帐号

After about 5 post queries the site is requesting captcha (post answer = {"captcha_needed": true, "error": "captcha-check-failed"})在大约 5 个帖子查询后,该网站正在请求验证码(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.我尝试使用 fake_useragent 来避免验证码,我还尝试使用 selenium 和 chrome web 驱动程序,但没有帮助。 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.我正在创建一个 Discord 机器人,所以忽略与其相关的所有代码。 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).主要问题是,即使我使用 chrome web 驱动程序解决了问题,我也不确定它是否适用于托管服务(例如 heroku)。 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.当我尝试手动获取此页面时,我可以简单地单击验证码框并通过它,但是当我尝试 chrome web 驱动程序时,它需要额外的图片选择。 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.我希望有人以前遇到过类似的问题并且可以帮助我。

Because the iframe is on a different domain you also have to launch chrome with '--disable-web-security':由于 iframe 位于不同的域中,因此您还必须使用“--disable-web-security”启动 chrome:

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

Then switch to the iframe to click:然后切换到iframe点击:

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.您可能需要调整 css。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM