【问题标题】:Using playwright for Python, how do I wait for two different selectors/handles at the same time and take the first successful match?使用 playwright for Python,我如何同时等待两个不同的选择器/句柄并取得第一个成功的匹配?
【发布时间】:2022-07-04 21:39:27
【问题描述】:

使用 Python 版 Playwright,我需要捕捉页面是否显示获胜者失败者消息。

我可以等待获胜者消息显示如下:

new_selector = "text=Your are a winner"
page.wait_for_selector(new_selector)
handle = query_selector(new_selector)
# do something with handle

但是我能做些什么来等待两个不同的事情呢? (不仅是文本,任何类型的选择器)

我可以试试无限循环:

new_selector1 = "text=Your are a winner"
new_selector2 = "text=Better luck next time"

while True:
    handle = query_selector(new_selector1)
    if handle:
        break  
    handle = query_selector(new_selector2)
    if handle:
        break   
    time.sleep(0.25) 
    # write my own timeout here

# do something with handle

但是剧作家中有什么东西可以让我等待两个句柄 并参加第一场比赛

【问题讨论】:

  • 有链接就好了,没听说过。

标签: python playwright webautomation playwright-python


【解决方案1】:

您可以传递以逗号分隔的两个选择器。 Ref.

new_selector = "text=Your are a winner, text=Better luck next time"
handle = query_selector(new_selector)

【讨论】:

    猜你喜欢
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多