问题
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:
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.
来源:https://stackoverflow.com/questions/44370831/get-real-xml-source-in-selenium-chrome-driver