How to read XML using XPath in Java

You need something along the lines of this: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(<uri_as_string>); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile(<xpath_expression>); Then you call expr.evaluate() passing in the document defined in that code and the return type you are expecting, and cast the result … Read more

XPath contains(text(),’some string’) doesn’t work when used with node with more than one Text subnode

The <Comment> tag contains two text nodes and two <br> nodes as children. Your xpath expression was //*[contains(text(),’ABC’)] To break this down, * is a selector that matches any element (i.e. tag) — it returns a node-set. The [] are a conditional that operates on each individual node in that node set. It matches if … Read more

How to select the first element with a specific attribute using XPath

Use: (/bookstore/book[@location=’US’])[1] This will first get the book elements with the location attribute equal to ‘US’. Then it will select the first node from that set. Note the use of parentheses, which are required by some implementations. Note, this is not the same as /bookstore/book[1][@location=’US’] unless the first element also happens to have that location … Read more

Is there a way to get element by XPath using JavaScript in Selenium WebDriver?

You can use document.evaluate: Evaluates an XPath expression string and returns a result of the specified type if possible. It is w3-standardized and whole documented: https://developer.mozilla.org/en-US/docs/Web/API/Document.evaluate function getElementByXpath(path) { return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; } console.log( getElementByXpath(“//html[1]/body[1]/div[1]”) ); <div>foo</div> https://gist.github.com/yckart/6351935 There’s also a great introduction on mozilla developer network: https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript#document.evaluate Alternative version, using XPathEvaluator: … Read more

Getting attribute using XPath

How could I get the value of lang (where lang=eng in book title), for the first element? Use: /*/book[1]/title/@lang This means: Select the lang attribute of the title element that is a child of the first book child of the top element of the XML document. To get just the string value of this attribute … Read more

How can I find an element by CSS class with XPath?

This selector should work but will be more efficient if you replace it with your suited markup: //*[contains(@class, ‘Test’)] Or, since we know the sought element is a div: //div[contains(@class, ‘Test’)] But since this will also match cases like class=”Testvalue” or class=”newTest”, @Tomalak’s version provided in the comments is better: //div[contains(concat(‘ ‘, @class, ‘ ‘), … Read more

How can I match on an attribute that contains a certain string?

Here’s an example that finds div elements whose className contains atag: //div[contains(@class, ‘atag’)] Here’s an example that finds div elements whose className contains atag and btag: //div[contains(@class, ‘atag’) and contains(@class ,’btag’)] However, it will also find partial matches like class=”catag bobtag”. If you don’t want partial matches, see bobince’s answer below.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)