2

I have been trying to click on a button for the following html by using python (selenium webdriver)

<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>

I am trying to click on the second button in the above html.

I am using find_element_by_tag because i think for html <button> tag it should work .I am using following code snippet to click on it , but getting-:element not visible

driver.find_element_by_tag_name("button").click()

Any help will be appreciated!!

gaurav tyagi
  • 25
  • 1
  • 10

2 Answers2

1

Selenium with Python documentation UnOfficial

Hii there

Selenium provides the following methods to locate elements in a page:

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

To find multiple elements (these methods will return a list):

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 is how your code should be

#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()
  • This way of finding elements is now deprecated in Selenium 4. See https://stackoverflow.com/a/69875984/2154717. The answer is still correct for Selenium 3. – Arete Jan 05 '22 at 10:50
0

There is probably more than one button tag on the page, try to be more explicit, like:

driver.find_element_by_class_name("cssm-Button_primary_1gH9y").click()

or even better, give the button an ID and use

driver.find_element_by_id("the-button-id").click()
Anthony L
  • 2,159
  • 13
  • 25
  • Thanks fo ryour reply , yes you are right and there are two html button and I have used find_element_by tag and find_elements_by_tag, both to acsess it .but still cant click on the second button – gaurav tyagi Mar 02 '18 at 05:44
  • Are you able to give the button an id and click on that? – Anthony L Mar 02 '18 at 05:46
  • hey !! thanks anthony, the code snippet you have provided `driver.find_element_by_class_name("cssm-Button_primary_1gH9y").click()` ..it worked for me , i am new to selenium and still clueless about why my ***find_Element_by_tag*** is not working? I agree there are 2 buttons on the page but as per the selenium , it must have clicked on the first button which is available to it while rendering the page , but in my case none of them is clicked . – gaurav tyagi Mar 02 '18 at 05:53
  • No problem - I'm glad it helped. The error is giving an indication of what is happening. The first button selenium is encountering is invisible, so unclickable. – Anthony L Mar 02 '18 at 05:57