Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在Python Selenium中处理reCAPTCHA_Python 3.x_Selenium_Iframe_Recaptcha_Webdriverwait - Fatal编程技术网

Python 3.x 在Python Selenium中处理reCAPTCHA

Python 3.x 在Python Selenium中处理reCAPTCHA,python-3.x,selenium,iframe,recaptcha,webdriverwait,Python 3.x,Selenium,Iframe,Recaptcha,Webdriverwait,我需要使用python selenium自动化一个网页,但它遇到了另一个框架中的reCaptcha。我想解决captcha,并在解决reCaptcha后,通过单击登录按钮继续脚本;但是,这会变得很棘手,因为涉及到一个框架,框架需要切换回默认内容。在这方面有人能帮我吗 from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.support import ex

我需要使用python selenium自动化一个网页,但它遇到了另一个框架中的reCaptcha。我想解决captcha,并在解决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()
填写电子邮件和密码字段以单击后,您可以使用以下选项:

  • 代码块:

    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()
    
  • 浏览器快照:


不要切换到iframe


您需要的所有内容都在主上下文中的
#g-recaptcha-response
[data sitekey]
中。

您只需等待复选框显示完成图标,然后等待就结束了。

就像下面的代码一样

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

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

尝试这样做:

创建获取主窗口的变量:

mainWin = driver.current_window_handle
driver.switch_to.window(mainWin)
当您需要切换到主窗口时:

mainWin = driver.current_window_handle
driver.switch_to.window(mainWin)

谢谢你的帮助。解决reCaptcha后,如何切换回主框架并继续脚本?这就是我遇到麻烦的地方。@AsadUllah用切换回主框架并单击登录按钮的步骤更新了答案。查看更新的答案并让我知道状态。是否等待我解决验证码,然后自动继续脚本?因为我在python编译器中运行了这个脚本,它只需在单击验证码后单击登录按钮。如何使用这些按钮来单击recaptcha并继续脚本?设置
#g-recaptcha-response
内部文本,然后单击该按钮。有关更多信息,请参阅2captcha的文档。是否需要等待我完成验证码,然后继续脚本?您需要解决验证码,然后输入,然后提交。谢谢。当我手动完成验证码后,我希望它能继续工作。