0

I have tried many ways some of them works fine in chrompath extension but not in script tried this xpath as well

//table[@id = 'level3InnerTable_1']/tbody/tr[@id = 'Billable Client']/td[1]/input

I can't use input id because it's dynamic. I Want to get id of input tag by starting traversing from ID of <tr> i.e Billable Client

I am using selenium java Eclipse with Chrome

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • What's the problem with what you have? – Guy Jan 14 '20 at 08:10
  • Xpath is not working in any formate .want to get this dynamic id="SelectionCheckboxInner-0" starting from static id of ="Billable Client" – Hamza Manzoor Jan 14 '20 at 08:13
  • *not working* doesn't mean much. What happens when you try it? if you have an exception post the stuck trace. – Guy Jan 14 '20 at 08:17
  • org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//table[@id = 'level3InnerTable_1']/thead/tbody/tr[@id = 'Billable Client ']/td[1]/input"} (Session info: chrome=79.0.3945.117) – Hamza Manzoor Jan 14 '20 at 08:26

2 Answers2

1

To get the value of id attribute induce WebDriverWait() and visibilityOfElementLocated() and following xpath options.

XPATH 1:

new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[contains(., 'Bilable to Client')]/preceding::input[1]"))).getAttribute("id");

OR

XPATH 2:

new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[contains(., 'Bilable to Client')]/preceding-sibling::td[1]/input"))).getAttribute("id");

OR

XPATH 3:

new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@id='level3InnerTable_1']//tr[@id='Billable Client'][.//td[.//input]]]//input"))).getAttribute("id");

Note: If you get timeout exception from all them then check if there any iframe on the page.

KunduK
  • 32,888
  • 5
  • 17
  • 41
0

To retrieve the value of the ID attribute of the <input> tag referencing the <tr> tag with ID value as Billable Client you have to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use either of the following Locator Strategies:

  • cssSelector:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("tr#Billable Client>td>input.selectedrow[name*='chk']"))).getAttribute("id"));
    
  • xpath:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//tr[@id='Billable Client']/td/input[@class='selectedrow' and contains(@name, 'chk')]"))).getAttribute("id"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352