我正在尝试访问此页面上的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
要在reCAPTCHA click()
上调用checkbox,您需要:
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()
浏览器快照:
tl;医生