我正在嘗試查找并單擊iframe 上的關閉按鈕。
<modal-container class="modal fade show" role="dialog" tabindex="-1" style="display: block;" aria-modal="true"><div role="document" class="modal-dialog modal-xl"><div class="modal-content"><div _ngcontent-gtk-c9="" class="modal-header card-header modal-header-news-expanded"><h4 _ngcontent-gtk-c9="" align="center" class="modal-title col-11 text-center">Article Title PDF </h4><button _ngcontent-gtk-c9="" aria-label="Close" class="close close_white_color" type="button"><span _ngcontent-gtk-c9="" aria-hidden="true">×</span></button></div><br _ngcontent-gtk-c9=""><div _ngcontent-gtk-c9="" class="container-fluid"><!----><div _ngcontent-gtk-c9="" class="row"><!----><div _ngcontent-gtk-c9="" class="col-md-12 ng-star-inserted"><!----><iframe _ngcontent-gtk-c9="" id="pdf_iframe_outside_modal" src="link.com" class="ng-star-inserted" cd_frame_id_="337af673cf64fd1888fcd2afe645984c"></iframe><!----></div></div><!----></div></div></div></modal-container>
我嘗試了以下方法:
嘗試1:
close = driver.find_element_by_xpath("//button[@aria-label=Close']").click()
嘗試2:
close = driver.find_element_by_xpath("//button[@class='close close_white_color']").click()
嘗試3:
close = driver.find_element_by_xpath("//button[contains(@class,\"close close_white_color\")]").click()
這給出了以下錯誤
錯誤 1:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@aria-label='Close']"}
錯誤2:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='close close_white_color']"}
錯誤3:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(@class,"close close_white_color")]"}
我能夠與 iframe 互動,但無法找到此按鈕。任何建議,將不勝感激
uj5u.com熱心網友回復:
這里有幾件事:
- 該元素不在iframe 內
- click()不回傳任何內容。
解決方案
所需元素位于Modal Dialog Box 中,要單擊可點擊元素,您需要為element_to_be_clickable()引發WebDriverWait,您可以使用以下任一定位器策略:
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.close.close_white_color[aria-label='Close'] > span"))).click()使用XPATH:
//button[@class='close close_white_color' and @aria-label='Close']/span注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/394378.html
標籤:Python 硒 路径 css-选择器 网络驱动程序等待
上一篇:我收到此錯誤“過時的元素參考:元素未附加到頁面檔案”,
下一篇:Selenium無資料輸出
