Home > Mobile >  How to pass a variable dynamically to a URL in playwright page.goto function?
How to pass a variable dynamically to a URL in playwright page.goto function?

Time:07-01

I want to pass a variable in the URL, here is my code:

   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)

But when I printed the URL it came like this: 'https://abc.name.co/admin/7Bid%7/reports/stats' like this in the {} block -> "{id}"

Can you please review my approach and let me know what am I doing wrong? Thanks!

CodePudding user response:

You are very close, so to embed the string value from the array you have to use the formatted string literals, something like this:

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)
  • Related