在Selenium Chrome驱动程序中获取真实的XML源 [英] Get real XML source in Selenium Chrome Driver
问题描述
我正在使用selenium和ChromeDriver测试XML响应.
I am using selenium and ChromeDriver to test a XML response.
响应如下:
<?xml version="1.0" encoding="UTF-8"?>
<d>test</d>
但是,如果我get
用硒表示该URL,chrome将自动呈现XML,从而使page_source
变脏.
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...'
(您可以看到chrome在页面源代码中添加了"XML查看器")
(You can see that chrome adds a "XML viewer" to the page source)
获取XML真正来源的最佳实践是什么?
What is the best practice to get the real source of the XML?
ps.此XML是由chrome扩展返回的,我将使用硒进行测试,因此使用requests
或urllib
"不是解决方案.
ps. This XML is returned by a chrome extension I will use selenium to test, so "use requests
or urllib
" is not a solution.
推荐答案
好的,我的解决方法是:
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.
这篇关于在Selenium Chrome驱动程序中获取真实的XML源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!