我一直试图通过使用python(selenium webdriver)点击以下html的按钮
<div class="cssm-TopNav_printIcon_2VzB_">
<button type="button" aria-label="Print" title="Print" class="cssm-Button_button_2a549 cssm-Button_secondary_22F7i cssm-Button_sm_EPQ2U">
<div class="" aria-hidden="true" color="black" style="display: inline-block; height: 16px; width: 16px; min-height: 16px; min-width: 16px; max-height: 16px; max-width: 16px;">
<svg viewBox="0 0 24 24" class="" focusable="false">
<g fill="#3C3F51">
<path d="M19 8H5c-1.66 0-3 1.34-3 3v6h4v4h12v-4h4v-6c0-1.66-1.34-3-3-3zm-3 11H8v-5h8v5zm3-7c-.55 0-1-.45-1-1s.45-1 1-1 1 .45 1 1-.45 1-1 1zm-1-9H6v4h12V3z">
</path>
<path d="M0 0h24v24H0z" fill="none">
</path></g></svg></div></button>
<button type="button" class="cssm-Button_button_2a549 cssm-Button_primary_1gH9y cssm-Button_sm_EPQ2U">
<!-- react-text: 314 -->View Custody List<!-- /react-text -->
</button>
我想点击上面html中的第二个按钮。
我正在使用find_element_by_tag因为我认为html <button>
标签它应该可以工作。我使用下面的代码片段来点击它,但是get-:元素不可见
driver.find_element_by_tag_name("button").click()
任何帮助将不胜感激!!
Selenium with Python documentation UnOfficial
这个存储库
Selenium提供了以下方法来定位页面中的元素:
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
要查找多个元素(这些方法将返回一个列表):
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
这就是你的代码应该如何
#this code will get the list of tags and select the second tag from the list
element = driver.find_elements_by_tag_name('button')[1]
#this will click the element
element.click()
页面上可能有多个按钮标记,尝试更明确,如:
driver.find_element_by_class_name("cssm-Button_primary_1gH9y").click()
甚至更好,给按钮一个ID并使用
driver.find_element_by_id("the-button-id").click()