Java Selenium:如何解析页面源代码。

时间:2017-05-12 22:35:37

标签: java xml selenium-webdriver

我将以下XML仅作为页面上显示的输出。我将其作为页面源读取并使用文档构建器解析每个节点值。但不幸的是,我无法读取任何值。节点列表计数仅给出零(0)。

以下是我的代码

String response = driver.getPageSource();

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();      
Document doc = dBuilder.parse(new InputSource(new StringReader(response)));
doc.getDocumentElement().normalize();

NodeList nList;
nList = doc.getElementsByTagName(words[0]);     //words[0]="exceptionList"
System.out.println("nList " + nList.getLength()); -- gives total length as zero
nList = doc.getElementsByTagName("exceptionList");
System.out.println("nList " + nList.getLength()); -- gives total length as zero

和XML在这里供参考。

<Resultant>
    <exceptionDetails>
        <exceptionList>
            <code>ABC</code>
            <message>Invalid Value</message>
        </exceptionList>
        <exceptionList>
            <code>ABZ</code>
            <message>Invalid Structure</message>
        </exceptionList>
    </exceptionDetails>
    <Result>
        <code>1234</code>
        <Details>
            <Detail>
                <System type="A">Admin</System>
                <Type>full</Type>
                <Date>2010-02-08</Date>
            </Detail>
            <Detail>
                <System type="B">Beneficiary</System>
                <Type>full</Type>
                <Date>2015-10-05</Date>
            </Detail>
            <Detail>
                <System type="C">Customer</System>
                <Type>Partial</Type>
                <Date>2010-11-01</Date>
            </Detail>
        </Details>
    </swiftBic>
</Resultant>

我可以使用getAttribute获取类型的值,但无法获取任何节点值。请在这里帮忙,并在我出错的地方纠正我。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


public class FindElements {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException{

        String response =" <Resultant> " +
   " <exceptionDetails> " +
       " <exceptionList> " +
           " <code> ABC</code> " +
           " <message> Invalid Value</message> " +
       " </exceptionList> " +
       " <exceptionList> " +
           " <code> ABZ</code> " +
           " <message> Invalid Structure</message> " +
       " </exceptionList> " +
   " </exceptionDetails> " +
   " <Result> " +
       " <code> 1234</code> " +
       " <Details> " +
           " <Detail> " +
               " <System type=\"A\"> Admin</System> " +
               " <Type> full</Type> " +
               " <Date> 2010-02-08</Date> " +
           " </Detail> " +
           " <Detail> " +
               " <System type=\"B\"> Beneficiary</System> " +
               " <Type> full</Type> " +
               " <Date>2015-10-05</Date> " +
           " </Detail> " +
           " <Detail> " +
               " <System type=\"C\"> Customer</System> " +
               " <Type> Partial</Type> " +
               " <Date>2010-11-01</Date> " +
           " </Detail> " +
       " </Details> " +
   " </Result> " +
"</Resultant> " ;

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();      
        Document doc = dBuilder.parse(new InputSource(new StringReader(response)));
        doc.getDocumentElement().normalize();

        NodeList nList;
        nList = doc.getElementsByTagName("exceptionList");   
        System.out.println("nList " + nList.getLength());
        nList = doc.getElementsByTagName("Details");
        System.out.println("nList " + nList.getLength());


    }
} 

输出如下:

nList 2
nList 1

你的xml中也有一些错误:

</Result>失踪了 </swiftBic>没有开头标记。