leedm777
leedm777

Reputation: 24052

How can I click the button next to a particular element in Selenium?

I'm trying to write Selenium tests for HTML that's structured like this:

<table>
  <tr id="row-1">
    <td><span class="some_data">what I'm looking for</span></td>
    <td><button class="doSomething" onclick="..."></button></td>
  </tr>

  <tr id="row-n">
    <td><span class="some_data">not what I'm looking for</span></td>
    <td><button class="doSomething" onclick="..."></button></td>
  </tr>
</table>

I need the test to click the button that's in the same row as what I'm looking for. I can find the span that it's in (driver.find_elements_by_xpath('//span[text() = "what I'm looking for"]')), but I don't know how to get from there to the corresponding button.

I'm using the Selenium Python bindings, version 2.21.2.

Upvotes: 0

Views: 1367

Answers (1)

unutbu
unutbu

Reputation: 880279

Try:

//tr[td/span[text() = "what I'm looking for"]]/td/button

If finds the tr which has a td/span with the text "what I'm looking for", then moves from that tr to the td/button inside it.

Upvotes: 3

Related Questions