当前位置: 首页 > 知识库问答 >
问题:

找到reCAPTCHA元素并单击它-Python+Selenium

曾修真
2023-03-14

我的代码如下所示:

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”

拜托,帮忙修一下。

共有1个答案

单于帅
2023-03-14

使用以下二进制文件集:

  • 硒v3.141.0
  • ChromeDriver V80.0
  • Chrome版本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()

>

  • 诱导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。我需要单击复选框验证码:在此处输入图片说明 我的代码如下所示: During the execution, there is an error: driver.find_elements_by_xpath(‘//*[@id=”recaptcha- anc

    • 问题内容: 我正在寻找网站。在检查元素中看到以下内容: 需要获取并按标题单击元素。像这样: 但是通过标题。 问题答案: 就像巴拉克·马诺斯(Barak Manos)所说,答案是: 以[0]结尾的情况是列表。

    • 如何使用selenium Python从主机A(在本例中为jsfiddle.net)单击此处的红色按钮?(java脚本限制策略错误,不让我去做)。我也不想直接点击红色按钮。谢了。

    • 问题内容: 我目前正在从事一个自动填写表格的项目。填写表单后,出现下一个按钮,这就是为什么它给我一个错误。 我努力了: HTML: 错误: selenium.common.exceptions.ElementClickInterceptedException:消息:元素单击被拦截:元素在点(203,530)不可单击。其他元素将获得点击:…(会话信息:chrome = 76.0.3809.132)

    • 问题内容: 我试图单击“所有主题”和“所有状态”复选框,然后搜索结果。当我运行脚本时,会打开一个Chrome窗口,大小为1036x674。 如果我不理会窗口,则会出现元素点击拦截错误。如果我最小化或最大化窗口,则脚本可以正常工作。 我正在使用Selenium 3.141.0,chrome 76,chromedriver 76和python 3.6 但是我得到这个错误: ElementClickIn

    • 我搞不懂这个。 我在这个网站上工作,我使用Python中的selenium来单击类名“yellow showPopupUnder”下的第一个元素(屏幕的主要部分有20行关于房屋的黄色信息)。 当我让Selenium点击该行后,它会打开并显示更多信息。我对有“选中”和“未选中”框的部分感兴趣。这些复选框位于这样的div中: 未选中的框位于一个div中,如下所示: 我试着用几种方法联系他们: 我还尝试