gpt4 book ai didi

Python-剧作家 : Is there a way to introspect and/or run commands interactively?

转载 作者:行者123 更新时间:2023-12-05 04:20:21 25 4
gpt4 key购买 nike

我正在尝试从 Selenium 转移到 Playwright 以执行一些网络抓取任务。

也许我养成了让 Selenium 一边运行浏览器一边测试运行中的命令和选择器的坏习惯。

有没有什么方法可以使用 Playwright 实现类似的效果?

到目前为止,我所取得的成就是在控制台上运行 playwright,类似于此:

from playwright.sync_api import sync_playwright
with sync_playwright() as pw:
browser = pw.chromium.launch(headless=False)
page = browser.new_page()
page.goto('https://google.com')
page.pause()

我得到一个浏览器窗口和一个 Playwright Inspector - 从那里我的命令或变量都不会执行。

最佳答案

我会使用 can i run playwright outside of 'with'? 中的技术和 How to start playwright outside 'with' without context managers在交互式回复上:

PS C:\Users\foo\Desktop> py
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from playwright.sync_api import sync_playwright
>>> p = sync_playwright().start()
>>> browser = p.chromium.launch(headless=False)
>>> page = browser.new_page()
>>> page.goto("https://www.example.com")
<Response url='https://www.example.com/' request=<Request url='https://www.example.com/' method='GET'>>
>>> page.title()
'Example Domain'
>>> page.close()
>>> browser.close()
>>> p.stop()

如果您使用 page.pause(),请尝试在浏览器开发工具控制台中运行 playwright.resume() 以恢复 Python repl。

如果您真的需要从脚本而不是 Python repl 执行此操作,您可以使用 code interpreterroll your own ,但我会尽可能避免这种情况。

关于Python-剧作家 : Is there a way to introspect and/or run commands interactively?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74517390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com