如何在Selenium和Firefox中检索原始JSON数据?



如何使用Selenium和Firefox webdriver从Web请求中获取原始JSON文本?

我正在尝试测试JSON API,并确认特定数据是通过Firefox的Python Selenium接口返回的。如果我在 Chrome 中查看网址的网页源代码,则会看到以下内容:

{"id":10472}

但是,如果我在Selenium + Firefox中调用相同的URL并通过json_text = self.driver.page_source访问文档的源代码,我会得到:

<html platform="linux" class="theme-light" dir="ltr"><head><meta http-equiv="Content-Security-Policy" content="default-src 'none' ; script-src resource:; "><link rel="stylesheet" type="text/css" href="resource://devtools-client-jsonview/css/main.css"></head><body><div id="content"><div id="json">{"id":10472}</div></div><script src="resource://devtools-client-jsonview/lib/require.js" data-main="resource://devtools-client-jsonview/viewer-config.js"></script></body></html>

看起来Firefox正在检测JSON内容类型,并试图通过将其包装在一个花哨的HTML UI中来"帮助"我的内容类型。不幸的是,这使得以编程方式从Selenium中提取和验证JSON变得困难。如何禁用此功能?

为什么不直接找到带有 json 的WebElement呢?

self.driver.find_element_by_id('json').text # {"id":10472}

最新更新