如何从Selenium Webdriver Firefox中的`driver.page

编程入门 行业动态 更新时间:2024-10-27 21:14:34
本文介绍了如何从Selenium Webdriver Firefox中的`driver.page_source`获取HTTP请求的原始JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我浏览到 httpbin/headers ,我希望得到以下JSON响应:

If I browse to httpbin/headers I expect to get the following JSON response:

{ "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en-US,en;q=0.5", "Connection": "close", "Host": "httpbin", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0" } }

但是,如果我使用硒

from selenium import webdriver from selenium.webdriver.firefox.options import Options options = Options() options.headless = True driver = webdriver.Firefox(options=options) url = 'httpbin/headers' driver.get(url) print(driver.page_source) driver.close()

我知道

<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"><script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="viewer-config" src="resource://devtools-client-jsonview/viewer-config.js"></script><script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="json-viewer" src="resource://devtools-client-jsonview/json-viewer.js"></script></head><body><div id="content"><div id="json">{ "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en-US,en;q=0.5", "Connection": "close", "Host": "httpbin", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0" } } </div></div><script src="resource://devtools-client-jsonview/lib/require.js" data-main="resource://devtools-client-jsonview/viewer-config.js"></script></body></html>

HTML标记来自哪里?如何从driver.page_source获取HTTP请求的原始JSON响应?

Where do the HTML tags come from? How do I get the raw JSON response of a HTTP request from driver.page_source?

推荐答案

除了原始JSON响应外,driver.page_source还包含HTML,以漂亮地打印"浏览器中的响应.如果使用Firefox DOM和Style Inspector在浏览器中查看JSON响应的源,您将得到相同的结果.

In addition to the raw JSON response, driver.page_source also contains the HTML to "pretty print" the response in the browser. You'll get the same result, if you use the Firefox DOM and Style Inspector to view the source of the JSON response in the browser.

要获取原始JSON响应,您可以照常浏览HTML元素:

To get the raw JSON response you can navigate the HTML elements as usual:

print(driver.find_element_by_xpath("//div[@id='json']").text)

更多推荐

如何从Selenium Webdriver Firefox中的`driver.page

本文发布于:2023-11-25 13:39:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Webdriver   Selenium   Firefox   page   driver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!