使用 python 在 iframe 中点击按钮

clicking on button in iframe with python

我正在尝试使用 python selenium 访问网站。我将放置无法单击按钮的位置。有什么帮助吗? 请在下面找到我的代码:

from selenium import webdriver
from time import sleep

path_to_chromedriver = "chromedriver.exe"
driver = webdriver.Chrome(path_to_chromedriver)

url = 'https://www.mail.com/'
driver.get(url)

如何使用 css selector 单击按钮 "Agree and Continue"

元素 Agree and Continue 存在于 nestediframe 您需要先切换两个 iframe 然后再单击该元素。

诱导WebDriverWait()等待frame_to_be_available_and_switch_to_it() 诱导 WebDriverWait() 并等待 element_to_be_clickable()

driver.get("https://www.mail.com/")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.permission-core-iframe")))
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://plus.mail']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='onetrust-accept-btn-handler']"))).click()

您需要导入以下库。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

跳出你需要使用的iframe

driver.switch_to.default_content()