![](/img/trans.png)
[英]Unable to click on element within iframe using Selenium Python
[英]Python Selenium: Unable to find xpath to click on element within iframe
我正在使用此代碼,但無法使其與 xpath 一起使用:
browser = webdriver.Chrome(chrome_path)
browser.get("https://planetradio.co.uk/cool-fm")
time.sleep(5)
browser.find_element_by_xpath('//*[@id="notice"]/div[4]/button[2]').click()
元素ACCEPT ALL在<iframe>
中,因此您必須:
誘導WebDriverWait使所需的幀可用並切換到它。
誘導WebDriverWait使所需元素成為可點擊的。
您可以使用以下任一定位器策略:
使用CSS_SELECTOR :
driver.get('https://planetradio.co.uk/cool-fm/') 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='ACCEPT ALL']"))).click()
使用XPATH :
driver.get('https://planetradio.co.uk/cool-fm/') 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='ACCEPT ALL']"))).click()
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
瀏覽器快照:
您可以在以下位置找到一些相關的討論:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.