程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了找到reCAPTCHA元素并单击它— Python + Selenium大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决找到reCAPTCHA元素并单击它— Python + Selenium?

开发过程中遇到找到reCAPTCHA元素并单击它— Python + Selenium的问题如何解决?下面主要结合日常开发的经验,给出你关于找到reCAPTCHA元素并单击它— Python + Selenium的解决方法建议,希望对你解决找到reCAPTCHA元素并单击它— Python + Selenium有所启发或帮助;

Solution update (11-Feb-2020)

使用以下二进制文件集:

  • Selenium v3.141.0
  • ChromeDriver v80.0
  • Chrome Version 80.0

您可以使用以下更新的代码块作为解决方案:

from selenium import webdriver
from selenium.webdriver.support.ui import webdriverwait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

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.inipec.gov.it/cerca-pec/-/pecs/companIEs")
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()

Original solution

在https://www.inipec.gov.it/cerca-pec/-/pecs/companies要在reCAPTCHA复选框上 调用的URL中,您需要:click()

诱导webdriverwait获得所需的帧并切换到该帧。 诱导webdriverwait使所需的元素可单击。 您可以使用以下解决方案:

        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
    from selenium.webdriver.Chrome.options import Options

    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', Chrome_options=options)
    driver.get("https://www.inipec.gov.it/cerca-pec/-/pecs/companIEs")
    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[@class='recaptcha-checkBox goog-inline-block recaptcha-checkBox-unchecked rc-anchor-checkBox']/div[@class='recaptcha-checkBox-checkmark']"))).click()

解决方法

我需要一些帮助。有URL:https : //www.inipec.gov.it/cercapec/-/pecs/companies。我需要单击复选框验证码:在此处输入图片说明

我的代码如下所示:

import os,urllib.request,requests,datetime,time,random,ssl,json,codecs,csv,urllib
from urllib.request import Request,urlopen
from urllib.request import urlretrieve
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.chrome.options import Options

chromedriver = "chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=chromedriver,chrome_options=chrome_options)
driver.get("https://www.inipec.gov.it/cerca-pec/-/pecs/companies")
driver.switch_to_default_content()
element = driver.find_elements_by_css_selector('iframe')[1]
driver.switch_to_frame(element)

driver.find_elements_by_xpath('//*[@id="recaptcha-anchor"]/div[1]').click()

During the execution,there is an error:

Please,help to fix it.

大佬总结

以上是大佬教程为你收集整理的找到reCAPTCHA元素并单击它— Python + Selenium全部内容,希望文章能够帮你解决找到reCAPTCHA元素并单击它— Python + Selenium所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: