Keith Tyler
Keith Tyler

Reputation: 825

How to get Chrome to load XML raw?

I am trying to load a raw XML url in Chrome, and use XPath to search the resulting XML. (This is with Selenium as part of a test.)

I'm pretty sure this worked before, but currently, when I direct the browser to the XML URL, instead of dumping the XML raw, it is wrapping it in some HTML.

If I load the URL in Chrome it displays fine, a la raw text. If I look in Network at the Preview or Response tabs, I see that only raw XML was sent in the payload.

What I expect is:

<Foo xmlns="some.site.com">
<Bar>123456</Bar><Thingy>1</Thingy><Widget>4420.00</Widget><Hooby>29.00</Hooby>
</Foo>

However, when I open Inspector, I get:

<html>
    <head/>
    <body>
        <pre style="word-wrap: break-word; white-space: pre-wrap;">
            &lt;Foo xmlns="some.site.com"&gt;
            &lt;Bar&gt;123456&lt;/Bar&gt;&lt;Thingy&gt;1&lt;/Thingy&gt;&lt;Widget&gt;4420.00&lt;/Widget&gt;&lt;Hooby&gt;29.00&lt;/Hooby&gt;
            &lt;/Foo&gt;</pre>
    </body>
</html>

Thus when I try to retrieve a value from the XML (in this case let's say the content of <Bar>), and I use a locator of By.xpath("//Bar"), it throws No Such Element.

Edit: I verified this is also what Selenium is seeing by iterating over the elements matching "//*".

for (WebElement e : driver.findElements(By.xpath("//*"))) {
    System.out.println(e.getTagName());
}

output:

html
head
body
pre

How do I get Chrome to just render XML as XML and not wrap it in HTML?

I'm really pretty sure this worked previously.

Upvotes: 2

Views: 2114

Answers (1)

Alexey R.
Alexey R.

Reputation: 8676

Browser normally decides how to render a response depending on what is Content-Type header value of that response. For XML it has to be application/xml.

If your tests used to work fine then probably the server used to set such header before and now it does not. You have to decide if this is a defect or not.

One of the possible workarounds is to use proxy that would set this header to all the responses for the URLs which represent your XML content.

Upvotes: 1

Related Questions