I need to click on the last button (CurrentPageUp) in a row. xPath does not work since nr of buttons change frequently. I tried

 find_element_by_xpath('//*[@id="ShowProducts_PagesTop"]/span/button[11]/span')

This does work but nr of buttons change frequently, so I cannot use it

Tried to loop to last button and execute it, but failed as well. Tried to select name but is is an arrow '>' and not sure how to use the '>' equivalent in the code to find and click on it.

.... First range of button codes

    <button onclick="CurrentPageUp();" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">
        &gt;
    </span></button>
</span></div>
  • 1
    If you always want the last button, why doesn´t getting all buttons via find_elements_by_tag_name("button") and just clicking the last element in the list work? – godisalie92 Jul 19 '16 at 15:34
  • XPath should be your LEAST preferred method to locate any element. But for some reason is is FIRST for most people who are asking questions because they have problems locating elements. Go figure. – Peter M. Jul 19 '16 at 15:47
  • Try to find elements by CSS or even XPATH if you needed, and them input in match code. But as always, any of them have positive and negative. – G.Guy Feb 11 at 9:09

To find the last button using Xpath try this:

find_element_by_xpath('//*[@id="ShowProducts_PagesTop"]/span/button[last()]/span')

So instead of giving a number, you can select the last one.

Have you tried selectors for css or xpath like:

button[onclick*='CurrentPageUp']

or

//button[contains(@onclick, 'CurrentPageUp')]

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.