Selenium data-driven (XML parsing)
Getelementbytagname () can get a tag from a tag name. The object it obtains is stored in an array form. If the "caption" and "item" labels have more than one in the Info.xml file, you can specify the subscript of the array to get a label.
Root.getelementsbytagname (' caption ') is labeled caption a set of tags; getElementsByTagName ("P") [3]
Item (0). Getnodename () represents the first of a set of labels.
Item (2). Getnodename () represents the third of a group of labels.
The GetAttributes () method can get the attributes of an element, and getNamedItem () specifies the name of the property.
The Gettextcontent () method is used to get the data before the label pair.
HTML code
<?XML version= "1.0" encoding= "UTF-8"?><Catalog><Maxid>4</Maxid><Loginusername= "Pytest"passwd= "123456"><caption>Java</caption><ItemID= "4"><caption>Test</caption></Item></Login><ItemID= "2"><caption>Zope</caption></Item></Catalog>
Java Code One
1 Packageselenium.test;2ImportJava.io.File;3ImportJavax.xml.parsers.DocumentBuilder;4Importjavax.xml.parsers.DocumentBuilderFactory;5Importorg.w3c.dom.Document;6Importorg.w3c.dom.NodeList;7 8 Public classReadXML {9 Public Static voidMain (string[] args) {10Try {One File xmlfile =NewFile ("C:\\users\\happy\\desktop\\domo.xml");Documentbuilderfactory factory =documentbuilderfactory.newinstance ();Documentbuilder Builder =Factory.newdocumentbuilder ();Document doc =Builder.parse (xmlfile);NodeList ma = doc.getelementsbytagname ("Maxid");NodeList CA = doc.getelementsbytagname ("caption");NodeList it = doc.getelementsbytagname ("Item");System.out.println (Ma.item (0). Getnodename ());System.out.println (Ca.item (0). Getnodename ());System.out.println (It.item (0). Getnodename ());21}Catch(Exception e) {22e.printstacktrace ();23 }24 }25 }
Java Code II
11 Packageselenium.test;2233ImportJava.io.File;44ImportJavax.xml.parsers.DocumentBuilder;55Importjavax.xml.parsers.DocumentBuilderFactory;6677Importorg.python.constantine.platform.darwin.Sysconf;88Importorg.w3c.dom.Document;99Importorg.w3c.dom.NodeList;Ten10 One11 Public classReadXML { A12 Public Static voidMain (string[] args) { -13Try { -File xmlfile =NewFile ("C:\\users\\happy\\desktop\\domo.xml"); theDocumentbuilderfactory factory =documentbuilderfactory.newinstance (); -Documentbuilder Builder =Factory.newdocumentbuilder (); -Document doc =Builder.parse (xmlfile); -18//gets the value of the element/attribute in the specified label +NodeList login = doc.getelementsbytagname ("Login"); -System.out.println (Login.item (0). GetAttributes (). getNamedItem ("username")); +System.out.println (Login.item (0). GetAttributes (). getNamedItem ("passwd")); ANodeList CA = doc.getelementsbytagname ("caption"); at23//get the text content in a label -24 for(inti = 0; I < ca.getlength (); i++) { -25System.out.println (Ca.item (i). Gettextcontent ()); -26//loop to get the text content in a caption tag -27 } -28 in29}Catch(Exception e) { -30e.printstacktrace (); to31 } +32 } -33 the34}
Selenium Java read XML (Data driven)