【问题标题】:How to click on a button in python using selenium with aria-label如何使用带有 aria-label 的 selenium 在 python 中单击按钮
【发布时间】:2022-01-25 01:42:39
【问题描述】:

我正在尝试查找并单击 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 交互,但无法找到此按钮。任何建议将不胜感激

【问题讨论】:

  • 你能分享一个指向那个页面的链接,或者至少是那个页面的所有 HTML 和更多你的代码吗?
  • @Prophet 已添加扩展页面 HTML

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

这里有几件事:


解决方案

所需的元素在Modal Dialog Box 内,要点击可点击 元素,您需要将WebDriverWait 诱导为element_to_be_clickable(),您可以使用任一以下Locator Strategies:

  • 使用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
    

【讨论】:

  • 感谢@DebanjanB。你是对的,我在模态对话框中有一个 iframe。我想下载 iframe 中的 pdf,然后单击模态对话框中的“关闭”(如问题所示)。一旦我切换到 iframe 并下载 pdf,我将无法运行您的解决方案,它会给我一个错误 StaleElementReferenceException: stale element reference: element is not attach to the page document
猜你喜欢
  • 2021-06-30
  • 2020-08-26
  • 1970-01-01
  • 2019-10-29
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 2020-04-14
  • 2022-01-19
相关资源
最近更新 更多