【问题标题】:Dealing with reCAPTCHA in Python Selenium在 Python Selenium 中处理 reCAPTCHA
【发布时间】:2020-04-11 16:54:13
【问题描述】:

我需要使用 python selenium 自动化网页,但它遇到了另一个框架中的 reCaptcha。我想解决验证码,并通过单击登录按钮继续脚本,当 reCaptcha 已解决时;但是,这会变得很棘手,因为涉及到一个框架,并且该框架需要切换回默认内容。有人可以在这方面帮助我吗?

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
browser = webdriver.Chrome()
browser.delete_all_cookies()    
browser.maximize_window()
browser.get("https://developer.syntecx.org/ptcl_ebills/signin.php")
browser.switch_to.frame(browser.find_element_by_tag_name("iframe"))
browser.find_element_by_xpath("//*[@id='recaptcha-anchor']/div[1]").click()
time.sleep(20)
browser.switch_to_default_content()
browser.find_element_by_xpath("//*[@id='login']/button").click()

【问题讨论】:

    标签: python-3.x selenium iframe recaptcha webdriverwait


    【解决方案1】:

    填写电子邮件密码字段以单击后,您可以使用以下Locator Strategies

    • 代码块:

      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:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("https://developer.syntecx.org/ptcl_ebills/signin.php")
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#email"))).send_keys("Asad_Ullah@stackoverflow.com")
      driver.find_element_by_css_selector("input#password").send_keys("Asad_Ullah")
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor?']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.recaptcha-checkbox.goog-inline-block.recaptcha-checkbox-unchecked.rc-anchor-checkbox"))).click()
      driver.switch_to_default_content()
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary.block.full-width.m-b"))).click()
      
    • 浏览器快照:

    【讨论】:

    • 感谢您的帮助。解决 reCaptcha 后,如何切换回主框架并继续执行脚本?这就是我遇到麻烦的地方。
    • @AsadUllah 通过切换回主框架并单击 Login 按钮的步骤更新了答案。查看更新的答案,让我知道状态。
    • 是不是等我解决验证码,然后自动继续脚本?因为我在我的python编译器中运行了这个,点击验证码后它只是点击登录按钮。
    【解决方案2】:

    不要切换到 iframe。

    您需要的一切都在#g-recaptcha-response[data-sitekey] 中,它们都在主上下文中。

    【讨论】:

    • 我如何使用这些来点击recaptcha并继续脚本?
    • 设置#g-recaptcha-responseinnerText,然后点击按钮。有关更多信息,请参阅 2captcha 的文档。
    • 要不要等我完成验证码,然后再继续脚本?
    • 你需要解验证码,然后输入,然后提交。
    • 谢谢。当我手动完成验证码后,我希望它能够继续工作。
    【解决方案3】:

    您可以简单地等待复选框显示完成图标,然后等待结束

    如下代码所示

    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.Firefox()
    driver.get("http://somedomain/url_that_delays_loading")
    try:
        element = WebDriverWait(driver, 100).until(
            EC.presence_of_element_located((By.ID, "myDynamicElement"))
        )
    finally:
        driver.quit() here
    

    【讨论】:

      【解决方案4】:

      在这里,当您在登录页面中遇到 reCaptcha 时,我找到了解决方案。 简单的方法对我很有用。签出登录页面中的登录按钮。如果它失去了它的存在,那么你可以进入下一阶段。所以,我的解决方案不需要任何超时。很高兴自动化你的东西:)

          def check_exists_by_xpath(xpath):
              try:
                  self.driver.find_element_by_xpath(xpath)
              except NoSuchElementException:
                  return False
              return True
      
          login_btn_xpath = 'your login button xpath'
          while True:
              print('You can solve recaptcha during this period and hit the login button.')
              if check_exists_by_xpath(login_btn_xpath) is False:
                  break
          # TODO: you can go along from here!
         
      

      【讨论】:

        【解决方案5】:

        试试看:

        创建一个获取主窗口的变量:

        mainWin = driver.current_window_handle
        

        当你需要切换到主窗口时:

        driver.switch_to.window(mainWin)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-15
          • 1970-01-01
          相关资源
          最近更新 更多