如何使用python硒在Google验证码测试页上找到iframe

问题描述 投票:0回答:1

我正在尝试访问此页面上的iframe:https://www.google.com/recaptcha/api2/demo。这是您必须点击带有图片的iframe。 iframe出现,然后单击“我不是机器人”按钮。您必须使用硒才能起作用,如果您在chrome中手动进行操作,它将无法正常工作。

代码:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.google.com/recaptcha/api2/demo')
iframe = driver.find_element_by_xpath('//*[@id="recaptcha-demo"]/div/div/iframe')
driver.switch_to.frame(iframe)
driver.find_element_by_class_name('recaptcha-checkbox').click()
#choose new iframe with pictures - doesn't work
iframe = driver.find_element_by_xpath('/html/body/div[2]/div[4]/iframe')

这里是什么问题?tnx,vasja

python selenium selenium-webdriver iframe webdriverwait
1个回答
-2
投票

要在reCAPTCHA click()上调用,您需要:

  • WebDriverWait设置为所需的[[框架可用并切换到它]]。
  • WebDriverWait
  • 表示为所需的[[可点击的元素您可以使用以下Locator Strategies
  • 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 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:\Utility\BrowserDrivers\chromedriver.exe') driver.get('https://www.google.com/recaptcha/api2/demo') WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor']"))) WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.recaptcha-checkbox.goog-inline-block.recaptcha-checkbox-unchecked.rc-anchor-checkbox"))).click()

    浏览器快照:
  • captcha

tl;医生


您可以在以下位置找到相关的讨论:

Ways to deal with #document under iframe

© www.soinside.com 2019 - 2024. All rights reserved.