Home > OS >  Locating element in the dom with parent selenium xpath
Locating element in the dom with parent selenium xpath

Time:03-16

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.

Dom

CodePudding user response:

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.

CodePudding user response:

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();
    
  • Related