bitShredder
bitShredder

Reputation: 142

Python Webdriver - How can I select a radio button based upon the text of a separate label?

Using Python WebDriver, how can I select a radio button based upon the text of a separate element such as this label?

My current code has the ability to click the radio button by id. But I have no idea how it is possible to click it when I am depending on text from a completely separate element.

The following is a pic of what i'm up against.

All help is greatly appreciated! =)

enter image description here

Upvotes: 1

Views: 277

Answers (1)

Stan
Stan

Reputation: 3461

I've created a simplified example of your html on the screenshot, and assuming you want to get radio button which following sibling has "test text2", you need to use xpath axes:

<table>
<tbody>
<tr>
<td><input id="test_id" type="radio" value="123" /></td>
<td>test text</td>
<td>test text2</td>
<td>test text3</td>
</tr>
</tbody>
</table>

XPath would be: //tr/td[contains(text(),"test text2")]/preceding-sibling::td/input

The logic is:

  • get an element with specific text
  • go to previous sibling element (sibling == same level element)
  • now go deeper to td/input from there

Upvotes: 1

Related Questions