找到 reCAPTCHA 元素并点击它——Python + Selenium [英] Find the reCAPTCHA element and click on it -- Python + Selenium
问题描述
我需要一些帮助.有网址:
I need some help. There is URL: https://www.inipec.gov.it/cerca-pec/-/pecs/companies. I need to click checkbox Captcha:
我的代码如下:
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' 对象没有属性 'click'
driver.find_elements_by_xpath('//*[@id="recaptcha-anchor"]/div1').click() AttributeError: 'list' object has no attribute 'click'
请帮忙修复它.
推荐答案
解决方案更新 (11-Feb-2020)
使用以下二进制文件集:
Solution update (11-Feb-2020)
Using the following set of binaries:
- Selenium v3.141.0
- ChromeDriver v80.0
- Chrome 80.0 版
您可以使用以下更新后的代码块作为解决方案:
You can use the following updated block of code as a solution:
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()
<小时>
原始解决方案
在 URL https://www.inipec.gov.it/cerca-pec/-/pecs/companies
内调用 click()
上的 reCAPTCHA 复选框,您需要:
Original solution
Within the URL https://www.inipec.gov.it/cerca-pec/-/pecs/companies
to invoke click()
on the reCAPTCHA checkbox you need to:
- 诱导 WebDriverWait 使所需的框架可用并切换到它.
- 诱导 WebDriverWait 使所需的元素可点击.
您可以使用以下解决方案:
- Induce WebDriverWait for the desired frame to be available and switch to it.
- Induce WebDriverWait for the desired element to be clickable.
You can use the following solution:
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()
这篇关于找到 reCAPTCHA 元素并点击它——Python + Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!