Get real XML source in Selenium Chrome Driver

2019-09-15 03:17发布

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.

1条回答
霸刀☆藐视天下
2楼-- · 2019-09-15 03:56

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.

查看更多
登录 后发表回答