1

I'm using selenium with python. I have some element that is a checkbox, and I want to click on it. My problem is with getting that element, I have only the text In my case <td>xxxxx</td> and I want to get the element above it (the previous element, he is not is father, they are only adjacent) I tried this:

driver.find_element_by_xpath("//input[@type='checkbox']/following::td[text()='xxxxx']").click()

but it didn't work.

enter image description here

2
  • 1
    When you are viewing the elements like in your picture, right click on the element you want and mouse over copy then click on xpath. Just a tip I wish I knew when I started using selenium Commented Nov 2, 2018 at 18:50
  • 1
    I didn't know that, thanks :) Commented Nov 2, 2018 at 18:53

1 Answer 1

1

You can navigate from td with following xpath

driver.find_element_by_xpath("//td[text()='xxxxx']/preceding-sibling::td[1]/input[@type='checkbox']").click()

or you can try with parent tag and navigate nth td which has input tag like

driver.find_element_by_xpath("//td[text()='xxxxx']/parent::tr/td[1]/input[@type='checkbox']").click()
Sign up to request clarification or add additional context in comments.

6 Comments

It's returning the first column of that row, not the preceding one (the one above it)
In your html example, i see input tag is present in first td column
yes, in that case its true. but I tried it on different element, and it was always returning the first column
I have updated with parent navigation and also update preceding sibiling, that will give immediate sibiling
Do u tried this "//td[text()='xxxxx']/preceding-sibling::td[1]/input[@type='checkbox']" ? i m 100% sure this will find immediate preceding sibiling. Please check it in developer console $x("//td[text()='xxxxx']/preceding-sibling::td[1]/input[@type='checkbox']")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.