Sync_playwright()。start()似乎已悬挂

发布于 2025-02-11 08:18:33 字数 511 浏览 1 评论 0 原文

我写的简单代码:

from playwright.sync_api import sync_playwright
print('debug1')

p = sync_playwright().start()
print('debug2')
browser = p.firefox.launch(headless=False)
print('debug3')
page = browser.new_page()
page.goto("http://whatsmyuseragent.org/")
page.screenshot(path="example.png")
browser.close()

我得到的输出:

debug1

我是使用剧作家库的完整菜鸟,并且该程序只是挂在'sync_playwright()。start()'零件上。我已经尝试从Internet运行其他示例脚本,所有这些脚本都在这一行上冻结了。另外,我尝试使用更改浏览器并使用无头模式,所有这些模式都无法使用。

Simple code I wrote:

from playwright.sync_api import sync_playwright
print('debug1')

p = sync_playwright().start()
print('debug2')
browser = p.firefox.launch(headless=False)
print('debug3')
page = browser.new_page()
page.goto("http://whatsmyuseragent.org/")
page.screenshot(path="example.png")
browser.close()

And the output I get:

debug1

I am a complete noob in using playwright library, and the program just hangs on the 'sync_playwright().start()' part. I've tried running other example scripts from the internet, and all of them froze on this line. Also I've tried changing browsers and using headless mode, all of them didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终难愈 2025-02-18 08:18:33

您应该使用 sync_playwright 用作

from playwright.sync_api import sync_playwright
print('debug1')

with sync_playwright() as p:
    print('debug2')
    browser = p.firefox.launch(headless=False)
    print('debug3')
    page = browser.new_page()
    page.goto("http://whatsmyuseragent.org/")
    page.screenshot(path="example.png")
    browser.close()

输出

debug1
debug2
debug3

请参阅Python

编辑(特定于op)

作为最后的沟渠工作,请尝试以下步骤:

  1. 运行 pip install -u pip
  2. run pip卸载plawyright
  3. 运行 pip缓存删除playwright
  4. 运行 pip install playwright

请记住,如果您安装了多个版本的Python,请使用为PIP定义的PATH变量,用于Python 您想要安装剧作家。最后,尝试更改与剧作家一起使用的浏览器。因此,代替浏览器= p.firefox.launch(headless = false),使用 browser = p.chromium.launch(headless = false)>

You are supposed to use sync_playwright as a context managers:

from playwright.sync_api import sync_playwright
print('debug1')

with sync_playwright() as p:
    print('debug2')
    browser = p.firefox.launch(headless=False)
    print('debug3')
    page = browser.new_page()
    page.goto("http://whatsmyuseragent.org/")
    page.screenshot(path="example.png")
    browser.close()

Output

debug1
debug2
debug3

See playwright documentation for python here

Edit (specific to OP)

As a last ditch effort, try these steps in order:

  1. Run pip install -U pip
  2. Run pip uninstall plawyright
  3. Run pip cache remove playwright
  4. Run pip install playwright

Keep in mind that if you have installed multiple versions of python, use the PATH variable defined for pip for the version of python you want to install playwright in. Lastly, try changing the browser you use with playwright. So instead of browser = p.firefox.launch(headless=False), use browser = p.chromium.launch(headless=False)

自我难过 2025-02-18 08:18:33

也可以使用async_playwright尝试相同的方法:

import asyncio
from playwright.async_api import async_playwright
import time


# async def handle_dialog(dialog):
#     if dialog.message == 'You will be redirected to employer Login Portal.':
#       await dialog.dismiss()

async def main():
  # Create a browser instance.
  async with async_playwright() as playwright:
    browser = await playwright.firefox.launch(headless=False)
    #https://playwright.dev/docs/downloads
    context = await browser.new_context()
    # chromium_context = playwright.chromium.cast(context)
    # await chromium_context.set_browser_context_option("downloads", False)


    # Create a new page.
    page = await context.new_page()

    # Navigate to the specified URL.
    await page.goto('https://www.google.com/') 

    #https://stackoverflow.com/questions/72493916/closing-a-confirm-popup-with- 
    playwright-in-python/72513920#72513920
    page.on("dialog", lambda dialog: dialog.dismiss())
    # Wait for the page to load.
    await page.wait_for_load_state('networkidle', timeout=10000) # 10 secs 

    # Click on the "Sign In" button. 
    await page.wait_for_selector('css=#modal-content > span')
    mod =  page.locator('css=#modal-content > span')
    await mod.click()

    await page.wait_for_selector('css=body > div._bodyMID.pdGridM > div > 
    div.col-md-9.col-sm-8._action4Mob.homesearch > div.service-listnew > ul > 
    li.service1.single-ser > a')
    employerlogin =  page.locator('css=body > div._bodyMID.pdGridM > div > 
    div.col-md-9.col-sm-8._action4Mob.homesearch > div.service-listnew > ul > 
    li.service1.single-ser > a')
    await employerlogin.click()

   
    time.sleep(30)

    #await page.close()
    # close the context
    #await context.close()

    # Close the browser.
    #await browser.close()


 asyncio.run(main()) # one time callable

Same can be tried with async_playwright as well:

import asyncio
from playwright.async_api import async_playwright
import time


# async def handle_dialog(dialog):
#     if dialog.message == 'You will be redirected to employer Login Portal.':
#       await dialog.dismiss()

async def main():
  # Create a browser instance.
  async with async_playwright() as playwright:
    browser = await playwright.firefox.launch(headless=False)
    #https://playwright.dev/docs/downloads
    context = await browser.new_context()
    # chromium_context = playwright.chromium.cast(context)
    # await chromium_context.set_browser_context_option("downloads", False)


    # Create a new page.
    page = await context.new_page()

    # Navigate to the specified URL.
    await page.goto('https://www.google.com/') 

    #https://stackoverflow.com/questions/72493916/closing-a-confirm-popup-with- 
    playwright-in-python/72513920#72513920
    page.on("dialog", lambda dialog: dialog.dismiss())
    # Wait for the page to load.
    await page.wait_for_load_state('networkidle', timeout=10000) # 10 secs 

    # Click on the "Sign In" button. 
    await page.wait_for_selector('css=#modal-content > span')
    mod =  page.locator('css=#modal-content > span')
    await mod.click()

    await page.wait_for_selector('css=body > div._bodyMID.pdGridM > div > 
    div.col-md-9.col-sm-8._action4Mob.homesearch > div.service-listnew > ul > 
    li.service1.single-ser > a')
    employerlogin =  page.locator('css=body > div._bodyMID.pdGridM > div > 
    div.col-md-9.col-sm-8._action4Mob.homesearch > div.service-listnew > ul > 
    li.service1.single-ser > a')
    await employerlogin.click()

   
    time.sleep(30)

    #await page.close()
    # close the context
    #await context.close()

    # Close the browser.
    #await browser.close()


 asyncio.run(main()) # one time callable
2025-02-18 08:18:33

我遇到了这个问题,它是通过重新安装和升级PIP来解决的。我不知道为什么。

I had this issue and it was solved by reinstalling and upgrading pip. I have no idea why.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文