xmcp
xmcp

Reputation: 3742

Get real XML source in Selenium Chrome Driver

I am using selenium and ChromeDriver to test a XML response.

The response is like this:

<?xml version="1.0" encoding="UTF-8"?>
<d>test</d>

But If I get that URL in selenium, chrome will render the XML automatically, making the page_source dirty.

>>> from selenium import webdriver
>>> b=webdriver.Chrome()
>>> b.get('http://127.0.0.1/test.xml')
>>> b.page_source
'<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml"><head><style id="xml-viewer-style">/* Copyright 2014 The Chromium Authors. All rights reserved.\n * Use of this source code is governed by a BSD-style license that can be\n * found in the LICENSE file...'

(You can see that chrome adds a "XML viewer" to the page source)

What is the best practice to get the real source of the XML?

ps. This XML is returned by a chrome extension I will use selenium to test, so "use requests or urllib" is not a solution.

Upvotes: 1

Views: 2262

Answers (2)

AFFA Anas
AFFA Anas

Reputation: 1

You can use:

driver.find_element(by=By.ID, value="webkit-xml-viewer-source-xml").get_attribute('innerHTML')

Upvotes: 0

xmcp
xmcp

Reputation: 3742

All right, my solution is:

b.execute_script('return document.getElementById("webkit-xml-viewer-source-xml").innerHTML')

It's certainly not a good practice, but at least working.

Upvotes: 4

Related Questions