3 回答

TA貢獻1876條經驗 獲得超5個贊
該元素Tout Accepter位于 an 內<iframe>
,因此您必須:
誘導WebDriverWait等待所需的框架可用并切換到它。
誘導WebDriverWait以使所需元素可單擊。
您可以使用以下任一定位器策略:
使用
CSS_SELECTOR
:
driver.get('http://www.legorafi.fr/')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div#appconsent>iframe")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button--filled>span.baseText"))).click()
使用XPATH:
driver.get('http://www.legorafi.fr/')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"http://div[@id='appconsent']/iframe")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "http://button[contains(@class, 'button--filled')]/span[contains(@class, 'baseText')]"))).click()
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
瀏覽器快照:

TA貢獻1802條經驗 獲得超5個贊
該元素存在于 an 內部,iframe您需要切換iframe才能訪問該元素。
induce WebDriverWait() 和 wait for frame_to_be_available_and_switch_to_it() 以及下面的 css 選擇器
引發WebDriverWait()并等待element_to_be_clickable()并跟隨xpath
driver.get("http://www.legorafi.fr/")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"#appconsent>iframe")))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[text()='Tout Accepter']"))).click()
您需要導入以下庫
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

TA貢獻1854條經驗 獲得超8個贊
首先切換到橫幅框架,然后單擊接受按鈕:
from selenium import webdriver
url = "http://www.legorafi.fr/"
driver = webdriver.Chrome()
driver.get(url)
driver.switch_to.frame(2)
button = "/html/body/div/div/article/div/aside/section[1]/button"
driver.find_element_by_xpath(button).click()
(我使用 XPath 單擊按鈕,但這只是個人喜好)
希望有幫助!
添加回答
舉報