Search code examples
javahtmlseleniumgettext

Selenium java can't get html text


I have this HTML code:

<span id="slotTotal">
    <span id="slotUsed">4</span>
          /16
</span>

I want to get text /16 but when I try:

slotTot = driver.findElement(By.xpath("//*[@id='slotTotal']")).getText();

I get this Exception: org.openqa.selenium.InvalidSelectorException: invalid selector while trying to locate an element

How can I fix that? (If I get 4/16 is good too...) thanks in advance


Solution

  • To get text of SlotUsed

    String slotUsed= driver.findElement(By.xpath("//span[@id='slotUsed']")).gettext();
    System.out.println("Value of used slot"+slotUsed);
    

    To get SubTotal (subtotal is part of first span element)

      String total=driver.findElement(By.xpath(".//span[@id='slotTotal']")).getText();
      System.out.println("Total"+total);