简体   繁体   English

Selenium java 无法获取 html 文本

[英]Selenium java can't get html text

I have this HTML code:我有这个 HTML 代码:

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

I want to get text /16 but when I try:我想获取文本 /16 但是当我尝试时:

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

I get this Exception: org.openqa.selenium.InvalidSelectorException: invalid selector while trying to locate an element我收到此异常:org.openqa.selenium.InvalidSelectorException:尝试定位元素时选择器无效

How can I fix that?我该如何解决? (If I get 4/16 is good too...) thanks in advance (如果我得到 4/16 也很好......)提前谢谢

To get text of SlotUsed获取 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 :使用xpath使用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.有一个'.jar'和一个'javadoc'如果你能得到 HTML,那么从 HTML 页面中检索任何内容都很容易。 Perhaps Selenium is complete overkill.也许 Selenium 完全是矫枉过正。 It is generally only useful for complicated AJAX/JavaScript that you cannot pick out.它通常仅对您无法挑选的复杂 AJAX/JavaScript 有用。

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:上述程序将产生以下 output 到 UNIX / BASH ZEA89B68C34CE4A63C0FZ77E7

@cloudshell:~$ java so.y2019.Oct.q002.SO 
Slot Used: 4
Slot Total: /16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM