python - 如何使用 webdriver 选择正确的 i-frame

标签 python selenium captcha

给大家。 我有下一个网址:“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
import os.path
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
from bs4 import BeautifulSoup
from PIL import Image


proxy = "94.79.52.207:8080" # IP:PORT or HOST:PORT
chromedriver = "chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
options.add_argument('--proxy-server=%s' % proxy)
os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=chromedriver,       chrome_options=chrome_options)
driver.set_window_size(1050, 740)
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()

driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"#rc-imageselect > div.rc-footer > div.rc-controls > div.primary-controls")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="recaptcha-audio-button"]'))).click()

最佳答案

刚刚将 iFrame 更改为 XPath...如下所示:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[@title="recaptcha challenge"]')))

希望这对您有帮助!

关于python - 如何使用 webdriver 选择正确的 i-frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54456050/

相关文章:

python - 为什么以下代码在 C 和 Python 中给出不同的输出?

java - 如何在不使用 Maven 的情况下设置 Selenium WebDriver?

java - Selenium:不明白如何为 IMDB 热门电影编写正确的 xpath

PHP:从 javascript 重新加载验证码图像

javascript - 我无法使用 Javascript 验证验证码是否错误

python - 如何使用 Python 插件 reCaptcha 客户端进行验证?

python - 如何在 python 中不使用 find() 的情况下查找字符串中的索引

python - Django管理面板将None类型数据转换为空字符串

python - 使用 Python 将 Apache Kafka 与 Apache Spark Streaming 集成

python - 使用 python 和 selenium 以某种语言(英语)抓取谷歌代码段文本