编程语言
首页 > 编程语言> > 找到reCAPTCHA元素并单击它 – Python Selenium

找到reCAPTCHA元素并单击它 – Python Selenium

作者:互联网

我需要协助.
有URL:https://www.inipec.gov.it/cerca-pec/-/pecs/companies.
我需要点击复选框Captcha:
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”]/div07002’).click()
AttributeError: ‘list’ object has no attribute ‘click’

请帮助解决它.

解决方法:

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

标签:python,selenium,recaptcha,webdriver,webdriverwait
来源: https://codeday.me/bug/20190928/1825547.html