0

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

  • Does this solution help? https://stackoverflow.com/a/49909051/11700321 – EGC Oct 28 '19 at 22:39
  • No sorry :/ It just says it's an invalid xpath, but I get the xpath from chrome, and if it is an invalid xpath I don't know what's the valid one – Davide Melis Oct 28 '19 at 22:43
  • Have you tried: `slotTot = driver.findElement(By.xpath("//span[@id='slotTotal']")).getText();` ? – EGC Oct 28 '19 at 22:51
  • Have you tried String getText = ((JavascriptExecutor) driver).executeScript("return arguments[0].value;",driver.findElement(By.xpath("//span[@id='slotTotal']"))); – Chaitanya Pujari Oct 29 '19 at 06:40

3 Answers3

0

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);
SeleniumUser
  • 3,833
  • 2
  • 6
  • 23
0
String slotTot = driver.findElement(By.xpath("//span[normalize-space(@id='slotUsed']")).gettext();

Hope this will help you.

frianH
  • 6,710
  • 6
  • 17
  • 42
Amruta
  • 1,068
  • 1
  • 8
  • 18
0

Use xpath utilize following-sibling:

//span[@id='slotUsed']/following-sibling::text()[1]

Like below:

String slotTot = driver.findElement(By.xpath("//span[@id='slotUsed']/following-sibling::text()[1]")).getText();
frianH
  • 6,710
  • 6
  • 17
  • 42