简体   繁体   English

如何使用带有 aria-label 的 selenium 单击 python 中的按钮

[英]How to click on a button in python using selenium with aria-label

I am trying to find and click a close button on an iframe.我正在尝试查找并单击 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>

I have tried the following methods:我尝试了以下方法:

Try1:尝试1:

close = driver.find_element_by_xpath("//button[@aria-label=Close']").click()

Try2:尝试2:

close = driver.find_element_by_xpath("//button[@class='close close_white_color']").click()

Try3:尝试3:

close = driver.find_element_by_xpath("//button[contains(@class,\"close close_white_color\")]").click()

Which gives following error这给出了以下错误

Error1:错误1:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@aria-label='Close']"}

Error2:错误2:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='close close_white_color']"}

Error3:错误3:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(@class,"close close_white_color")]"}

I am able to interact with the iframe but unable to locate this button.我能够与 iframe 交互,但无法找到此按钮。 Any suggestions would be appreciated任何建议,将不胜感激

A couple of things here:这里有几件事:


Solution解决方案

The desired element is within a Modal Dialog Box , to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :所需的元素位于Modal Dialog Box中,要单击可点击元素,您需要为element_to_be_clickable()诱导WebDriverWait ,您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.close.close_white_color[aria-label='Close'] > span"))).click()
  • Using XPATH :使用XPATH

     //button[@class='close close_white_color' and @aria-label='Close']/span
  • Note : You have to add the following imports:注意:您必须添加以下导入:

     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.

相关问题 如何通过带有 Python Selenium 的 aria-label 找到并单击 JavaScript 按钮? - How to find and click a JavaScript button by aria-label with Python Selenium? 使用aria-label通过Python3和Selenium定位并单击元素 - Using aria-label to locate and click an element with Python3 and Selenium 如何使用 selenium 和 python 查找 aria-label - How to do find aria-label using selenium and python Python Selenium 单击按钮,没有名称、没有 id、没有文本、许多类和一个 aria-label - Python Selenium click button with no name, no id, no text, many classes, and an aria-label Python Selenium:访问 aria-label 信息 - Python Selenium: accessing aria-label information 如何在 python 中抓取 aria-label 文本? - How to scrape aria-label text in python? 我想通过 python selenium 从“aria-label”获取文本 - I want to get text from "aria-label" by python selenium 使用 Selenium 的 find 函数检索 aria-label 值 - Retrieving aria-label value using Selenium's find function 如何根据使用Selenium的html从使用xpath找到的元素中检索属性aria-label的值 - How to retrieve the value of the attribute aria-label from element found using xpath as per the html using Selenium python标签的Scrapy提取值 - Python Scrapy Extract Value of aria-label
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM