找到reCAPTCHA元素并点击 -- Python + Selenium

19

我需要一些帮助。 这是一个 URL:https://www.inipec.gov.it/cerca-pec/-/pecs/companies。 我需要点击验证码复选框: enter image description here

我的代码看起来像:

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()

执行过程中出现错误:

driver.find_elements_by_xpath('//*[@id="recaptcha-anchor"]/div1').click() AttributeError: 'list' object has no attribute 'click'

请帮助修复此问题。


3
您需要使用find_element_by_xpath来查找单个网页元素。 - Ratmir Asanov
2个回答

29

解决方案更新(2020年2月11日)

使用以下二进制文件集合:

  • 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()

原始解决方案

在网址https://www.inipec.gov.it/cerca-pec/-/pecs/companies中调用reCAPTCHA复选框上的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()

@MobrineHayde,请查看更新后的答案并告诉我状态。 - undetected Selenium
仍然无法工作:ElementClickInterceptedException: Message: element click intercepted: Element <iframe>..</iframe> is not clickable at point (277, 36). Other element would receive the click - AbdulKarim
@AbdulKarim 请尝试:driver.execute_script("arguments[0].click();", 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']"))))最后一行。 - Thomas Cannon
当您使用Selenium点击复选框时,是否会出现额外的图像识别验证码?我认为这很奇怪,因为当我使用常规Chrome浏览器单击时,不会发生这种情况,但是在Selenium中始终会发生。 - Sean
1
这个解决方案似乎无法与验证码v3一起使用。有什么办法吗?在这种情况下没有可点击的span。 - Eduard Grigoryev
谢谢,这个与reCAPTCHA v2完美配合。 - sakell

0

我解决了这个问题,你可以尝试使用你的落地页网址。

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.common.exceptions import SessionNotCreatedException

options = webdriver.ChromeOptions()
prefs = {"download.default_directory": download_dir}
options.add_experimental_option("prefs", prefs)
options.add_argument("--no-sandbox")
    
driver = webdriver.Chrome("/usr/bin/chromedriver", chrome_options = options)
driver.get("https://www.google.com/recaptcha/api2/demo")

driver.maximize_window()
price = driver.find_element_by_xpath("//div[@class='g-recaptcha']")
price_content = price.get_attribute('innerHTML')
start = str(price_content).find(";k=")+len(";k=")
end = str(price_content).find("&amp;co")
driver.implicitly_wait(20)
driver.execute_script("document.getElementById('g-recaptcha-response').style.display = '';")
recaptcha_text_area = driver.find_element_by_id("g-recaptcha-response")
    
recaptcha_text_area.clear()
recaptcha_text_area.send_keys(price_content[start:end])
    #.....................................................................................
    
button = driver.find_element_by_id("recaptcha-demo-submit")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接