英文:
How do I get the response associated with a specific GET request in playwright python?
问题
我想获取与GET请求相关联的响应。我正在使用Playwright Python。我在互联网上找不到解决我的问题的任何信息,这让我怀疑我对这个问题的理解是否有误。我有一个特定的GET请求,我正在尝试获取其JSON响应。我之所以使用Playwright,是因为我想触发GET请求,这些请求会在单击某些元素或滚动页面时发生。
我可以在浏览器检查器的网络选项卡中查看GET请求及其JSON响应,我也可以使用Insomnia发出请求并获取响应,但我不知道如何在Playwright中操作它。我是否必须重新发出请求并获取响应?这似乎有点不方便,因为应该有一种获取响应的方法,就像我可以使用以下代码在终端中显示GET和POST请求及其响应一样:
page.on("request", lambda request: print(">>", request.method, request.url))
page.on("response", lambda response: print("<<", response.status, response.url))
但我不知道哪个响应与请求相关联,因为在浏览器检查器中,您只需单击请求即可查看其响应。我想使用Python的requests库来完成,但考虑到我接下来要做的事情,这将非常不方便:使用Playwright来触发多个GET请求并获取它们的JSON响应。
我知道GET请求的URL,并希望只获取该特定请求的响应。
我是Playwright和编程的新手,如果您能引导我朝正确的方向前进,我将不胜感激。谢谢!
英文:
I want to get the response associated with a GET request. I am using playwright python. I haven't been able to find anything on the internet to solve my problem, which is making me wonder if my understanding of this is flawed. I have a specific GET request whose JSON response is what I am trying to get. The reason why I am using playwright is because I want to trigger the GET requests, which happens when certain elements are clicked or when the page is scrolled.
I can view the GET request and its JSON response in the browser inspector's Network tab, I can also make the request using insomnia and get the response but I don't know how to go about it in playwright. Do I have to remake the request and get the response that way? It seems kinda inconvenient as there has to be a way to get the response, as I am able to display the GET, POST requests and their responses in the terminal using:
page.on("request", lambda request: print(">>", request.method, request.url))
page.on("response", lambda response: print("<<", response.status, response.url))
But I don't know what response is associated with the request as, in the browser inspector, you can simply click on the request and view its response. I would do it using the python requests library but that would be very inconvenient considering what I am trying to do next: use playwright to trigger multiple GET requests and get their JSON responses.
I know the GET request URL, and would like to get the the response to that specific request only.
I am new to playwright and programming overall and would appreciate it if you could as much as nudge me in the right direction. Thanks!
答案1
得分: 0
响应类有一个 request 对象。因此,您可以从响应中获取请求。
英文:
The response class has a request object. So you can get the request from a response.
答案2
得分: 0
在我理解中,这可能会解决您的问题:在Playwright主页上捕获搜索请求并打印响应的示例:
更新:添加了如何返回原始JSON。
from playwright.sync_api import Page
import json
def test_catchSearchResponse(page: Page):
# 转到Playwright主页并点击搜索框:
page.goto("https://playwright.dev/")
page.get_by_role('button', name='Search').click()
# 捕获与填写搜索字段相关的响应:
with page.expect_response("**.algolia.net/**") as response:
# 在搜索框中填写一个字母以触发POST请求:
page.get_by_placeholder('Search docs').fill('A')
# 将响应的值打印为Python JSON对象:
print(response.value.json())
# 将响应的值打印为原始JSON:
print(json.dumps(response.value.json()))
了解更多信息 在文档中
英文:
In my understanding, this might solve your problem: Example of catching the search request on the Playwright homepage and print the response:
Update: added how to return raw json.
from playwright.sync_api import Page
import json
def test_catchSearchResponse(page: Page):
# Goto playwright homepage and press search box:
page.goto("https://playwright.dev/")
page.get_by_role('button', name='Search').click()
# Catching response associated with filling searchfield:
with page.expect_response("**.algolia.net/**") as response:
# Fill a letter in searchbox to trigger the post-request:
page.get_by_placeholder('Search docs').fill('A')
# Printing the value of the response as a python json object:
print(response.value.json())
# Printing the value of the response as raw json:
print(json.dumps(response.value.json()))
More about it in the docs
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论