将变量传递到python selenium中的driver.execute_script()中



我试图在一个新的选项卡中打开链接。我使用的方法如下。

driver.execute_script('''window.open("https://www.tracksellers.com/sellers/","_blank");''')

上面的代码是有效的,但我想在其中添加一些变量来构建完整的url,如下所示。

https://www.tracksellers.com/sellers/us/A294P4X9EWVXLJ/ankerdirect

这是我迄今为止尝试过的

driver.execute_script('''window.open("https://www.tracksellers.com/sellers/`{country}`/`{id}`","_blank");''')

但当它打开时,它添加了其他内容,这给了我如下所示的输出。

https://www.tracksellers.com/sellers/%60%7Bcountry%7D%60/%60%7Bid%7D%60

它在https://www.tracksellers.com/sellers之后添加%60%7Bcountry%7D%60/%60%7Bid%7D%60

您可以解析如下字符串:

我已经取了num字符串和name字符串,并用%s进行了解析

请参阅以下代码:

driver.get("https://www.tracksellers.com/")
num = "A294P4X9EWVXLJ"
name = "ankerdirect"
new_url = 'https://www.tracksellers.com/sellers/us/%s/%s/'  % (num, name)
driver.execute_script(f'''window.open("{new_url}","_blank");''')

相关内容

最新更新