Jonny Peppers
Jonny Peppers

Reputation: 169

How to click on repeated button on a page?

I have a table layout like this:

 <tbody>
    <tr>
      <td> 1 </td>
      <td> <button> Click me </button>
    </tr>
     <tr>
      <td> 2 </td>
      <td> <button> Click me </button>
    </tr>
    ....
  </tbody>

I only know the value of the first td (1,2, etc). Is there anyway to click on the appropriate second td (the button) while only knowing the first td? For example, can I dynamically get 2 and know to click on the second "Click me" button instead of the first one?

Upvotes: 0

Views: 74

Answers (2)

Yu Zhang
Yu Zhang

Reputation: 2149

yes there is way:

Basically, the buttons you want to click are siblings to the td elements you already know of.

Please try using locating an element via xpath sibling to locate those buttons:

//td[contains(text(),'1')]/following-sibling::*  //this reads, first locate an element of td type whose text() attribute contains '1', then find its immediate sibling.

//td[contains(text(),'2')]/following-sibling::*  //this reads, first locate an element of td type whose text() attribute contains '2', then find its immediate sibling.

Upvotes: 1

Buaban
Buaban

Reputation: 5137

You need an XPath:

twoClickMeButton  = driver.find_element(:xpath,"//td[text()='2']/../td/button")

Upvotes: 0

Related Questions