1

I have a website that has the same titled button several times on the page. I want to be able to click a certain one of these buttons. The only thing I can see that is different is the second number set in the ( ). In this example it is 123883445. Below is the results of the "inspect element"

<a style="position: relative;" class="vote_link vote" href="#" onclick="SST.vote_for(widget_47929421, 123883455, this); return false;" title="Vote">Vote</a>

How can I find then select this element by using the number 123883455?

1

There's a couple ways to go about this, depending on the behavior of the application.

Probably the most straightforward - assuming that the number 123883455 is always the same (not dynamically generated on page load) for the button you want - is to use a css attribute locator:

element = driver.find_element_by_css_locator("a[onclick*='123883455']")

That should return the single button you want to click. You could do a similar thing using xpath as well.

Or, if the button is always in the same position on the page (for example, it is always the 3rd "vote" button, you could do something like this...

element = driver.find_elements_by_css_locator("a.vote_link")[2]

That would get a collection of all the matching elements and return the third one.

0

I haven't tried it but my guess would be to get list of all "a" elements (find by tag) or all elements by class ("vote_link vote"), then loop over them, get the on_click property, and look for your number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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