我在 python https://zeitung.faz.net/打開這個網站時在彈出這個網站中打開一條訊息
driver.find_element_by_css_selector("#notice > div.message-component.message-row.sticky-element > div.message-component.message-row.row-no-margin-phone.cta-yep > div").click()
但不作業
uj5u.com熱心網友回復:
該元素ZUSTIMMEN在 an 內,<iframe>因此您必須:
誘導WebDriverWait使所需的幀可用并切換到它。
誘導WebDriverWait使所需元素成為可點擊的。
您可以使用以下任一定位器策略:
使用CSS_SELECTOR:
driver.get('https://zeitung.faz.net/') WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='ZUSTIMMEN']"))).click()使用XPATH:
driver.get('https://zeitung.faz.net/') WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='SP Consent Message']"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@title='ZUSTIMMEN']"))).click()
注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
參考
您可以在以下位置找到一些相關的討論:
- 通過 Selenium 和 python 切換到 iframe
- selenium.common.exceptions.NoSuchElementException:訊息:沒有這樣的元素:嘗試使用 selenium 單擊下一步按鈕時無法找到元素
- python中的硒:NoSuchElementException:訊息:沒有這樣的元素:無法找到元素
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/440979.html
標籤:Python 硒 框架 css 选择器 网络驱动程序等待
