在Selenium Chrome Driver中获取真正的XML源代码

时间:2017-06-05 14:05:52

标签: google-chrome selenium selenium-chromedriver

我正在使用selenium和ChromeDriver来测试XML响应。

响应是这样的:

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

但如果我get在selenium中使用该网址,则Chrome会自动呈现XML,从而使page_source变脏。

>>> 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查看器”)

获取XML真正来源的最佳做法是什么?

PS。此XML由chrome扩展程序返回,我将使用selenium进行测试,因此“使用requestsurllib”不是解决方案。

1 个答案:

答案 0 :(得分:0)

好的,我的解决方案是:

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

这当然不是一个好习惯,但至少可以工作。