[英]Python and Selenium: Locating and clicking a button for cookies within an 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
存在於nested
的iframe
,您需要先切換 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.