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

4
  • Does this solution help? stackoverflow.com/a/49909051/11700321 Commented Oct 28, 2019 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 Commented Oct 28, 2019 at 22:43
  • Have you tried: slotTot = driver.findElement(By.xpath("//span[@id='slotTotal']")).getText(); ? Commented Oct 28, 2019 at 22:51
  • Have you tried String getText = ((JavascriptExecutor) driver).executeScript("return arguments[0].value;",driver.findElement(By.xpath("//span[@id='slotTotal']"))); Commented Oct 29, 2019 at 6:40

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

please check upddated solution
is it working for you ? if yes then please accept my answer
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();

Comments

0
String slotTot = driver.findElement(By.xpath("//span[normalize-space(@id='slotUsed']")).gettext();

Hope this will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.