1

我正在为我尝试新事物,使用谷歌 colab 中的剧作家。
这种组合需要/强制异步编程。

我有一个上下文管理器,它能够处理名为“登录”的登录和注销。效果很好!

我试图访问的内部页面有数据集,没有链接,只有 div 可以点击。

定位器(我相信)工作正常,当与我假设的 .element_handles() 结合使用时应该返回多个元素。

from playwright.async_api import async_playwright
import asyncio
from IPython.display import Image

import nest_asyncio
nest_asyncio.apply()
# browser is set to webkit in the Login() context manager
...
async def loop_over_datasets(browser=None, page=None):
  print("starting")
  datasets = page.locator("div.horizontal.clickable")
  print("continuing")
  datasets = await asyncio.gather(datasets.element_handles())
  for ds in datasets:
    print(f'inside the loop, ds is {ds}')
    print("doesn't get here in tact")
    # for each dataset I want to launch a new page where the dataset is clicked but I'll settle for sync programming at this point.
    # new_page = await ds.click()
    # ds_page = await browser.new_page(new_page)
    # ds_page.click()


async def get_all_new_info():
  async with Login() as (b,l):
    await loop_over_datasets(browser=b,page = l)
asyncio.run(get_all_new_info()) #has to be killed manually or it will run forever.

在这条线上datasets = await asyncio.gather(datasets.element_handles()) gather()实际上没有工作await并且await永远不会返回,这意味着我没有“进入循环......”。

没有await我得到“ds”变量,但它不是我能做的任何事情。

这个应该怎么用?

4

1 回答 1

0

如果没有完整的代码,测试起来有点困难,但想分享一些可能有帮助的东西:

datasets = await asyncio.gather(datasets.element_handles())

据我在Playwright 文档 element_handles()返回中看到的<List[ElementHandle]>,您正在尝试将这个列表传递给asyncio.gather需要等待的对象,这些对象是协程、任务和期货,可能这就是它不起作用的原因,所以我就完成了

datasets = datasets.element_handles()

现在,我假设您希望以异步方式浏览这些数据集。您应该能够将 for 循环的内容放入协程中,并基于该创建将由gather.

async def process_dataset(ds):
    new_page = await ds.click()
    ds_page = await browser.new_page(new_page)
    ds_page.click()

tasks = []
for ds in datasets:
    tasks.append(asyncio.create_task(process_dataset(ds)))
    
await asyncio.gather(*tasks)
于 2022-02-23T20:27:11.720 回答