I am trying to select a checkbox from table td element, but Selenium is not finding the element, this is my xpath locator "//*/td/a[contains(.,'Samsung Galaxy Note 10.1')]//parent::td[last()]/input[@type='checkbox']"
. I need to click on input filed with type checkbox, depending on the text in the with the Galaxy text.
Asked
Active
Viewed 59 times
1

mikebrucks
- 59
- 8
-
1If I not accept an answer, maybe it was not solving my issue I will recheck the questions and will accept. – mikebrucks Mar 16 '22 at 12:58
2 Answers
1
You can use this xpath
//a[contains(text(),'Samsung GALAXY Note 10.1')]/../following-sibling::td[last()]//input[@type='checkbox']
You are looking for Samsung Galaxy Note 10.1
, instead, it is Samsung GALAXY Note 10.1
if this is unique //a[contains(text(),'Samsung GALAXY Note 10.1')]
then the XPath provided by me should work.

cruisepandey
- 28,520
- 6
- 20
- 38
1
The element is a JavaScript enabled element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following locator strategy:
Using xpath and index:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(., 'Samsung GALAXY Note 10.1')]//following::td[12]//input[starts-with(@id, 'ContentModify') and @type='checkbox']"))).click();
Using xpath and last():
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//tr[@class='ContentRowOdd']/td[.//a[contains(., 'Samsung GALAXY Note 10.1')]]//following::td[last()]//input[starts-with(@id, 'ContentModify') and @type='checkbox']"))).click();

undetected Selenium
- 183,867
- 41
- 278
- 352
-
never hardcode numbers in XPath `td[12]` this is not a good practice. – cruisepandey Mar 16 '22 at 12:40
-
@cruisepandey Please have a relook, it's not a number as such but an index with is inevitable when locating element with respect to some other element. Let me know your thoughts. – undetected Selenium Mar 16 '22 at 12:49
-
if the UI dev adds one more `td` in between, then `td[12]` would not work. The HTML shared by OP `last()` seems more sensible. Now I see that you've modified your answer with `last(), it's all good now. – cruisepandey Mar 16 '22 at 12:52
-
@cruisepandey _if the UI dev adds one more td in between_: None of my answer's are question specific. All are generalized answers which can benefit the broader audience. However answers are subjected to modification, please don't be suprised to see another new locator within a short while. – undetected Selenium Mar 16 '22 at 12:58