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
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);
String slotTot = driver.findElement(By.xpath("//span[normalize-space(@id='slotUsed']")).gettext();
Hope this will help you.
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();
There is a '.jar' and a 'javadoc' It is easy to retrieve anything from an HTML Page, if you can get the HTML. Perhaps Selenium is complete overkill. It is generally only useful for complicated AJAX/JavaScript that you cannot pick out.
import Torello.HTML.*;
import Torello.HTML.NodeSearch.*;
import Torello.Java.FileRW;
import java.util.*;
import java.io.IOException;
public class SO
{
public static void main(String[] argv) throws IOException
{
String html = FileRW.loadFileToString("so/y2019/Oct/q002/SO-Input.html");
// Convert the string to vectorized-html (a list)
Vector<HTMLNode> v = HTMLPage.getPageTokens(html, false);
// Get a copy of the OUTER <SPAN> ... </SPAN>
Vector<HTMLNode> slotTotalVec = InnerTagGetInclusive.first(v, "span", "id", TextComparitor.CN_CI, "slotTotal");
// Poll means to "REMOVE, and get a COPY OF" the INNER <SPAN> ... </SPAN>
Vector<HTMLNode> slotUsedVec = InnerTagPollInclusive.first(slotTotalVec, "span", "id", TextComparitor.CN_CI, "slotUsed");
// Util.textNodesString -> ignores all HTML Elements, and converts Text-Nodes to String
String slotUsedStr = Util.textNodesString(slotUsedVec).trim();
String slotTotalStr = Util.textNodesString(slotTotalVec).trim();
// Print it.
System.out.println("Slot Used: " + slotUsedStr + '\n' +
"Slot Total: " + slotTotalStr );
}
}
The above program will produce the following output to a UNIX / BASH Shell:
@cloudshell:~$ java so.y2019.Oct.q002.SO
Slot Used: 4
Slot Total: /16
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.