【问题标题】:How to pass a variable dynamically to a URL in playwright page.goto function?如何将变量动态传递给 playwright page.goto 函数中的 URL?
【发布时间】:2022-06-30 21:30:25
【问题描述】:

我想在 URL 中传递一个变量,这是我的代码:

   url_id = ["253443","456545"]
   for id in url_id :
       print("Inside For loop", id) # this print the correct id (253443)
       page.goto('https://abc.name.co/admin/{id}/reports/stats',timeout=0)

但是当我打印 URL 时,它是这样的: 'https://abc.name.co/admin/7Bid%7/reports/stats' 在 {} 块中像这样 -> "%7Bid%7D"

能否请您检查一下我的方法并让我知道我做错了什么?谢谢!

【问题讨论】:

    标签: python-3.x playwright playwright-python playwright-test


    【解决方案1】:

    您非常接近,因此要从数组中嵌入字符串值,您必须使用 formatted string literals,如下所示:

    url_id = ["253443", "456545"]
    for id in url_id:
        print("Inside For loop", id)  # this print the correct id (253443)
        page.goto(f"https://abc.name.co/admin/{id}/reports/stats", timeout=0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 2018-05-17
      • 1970-01-01
      相关资源
      最近更新 更多