robel
robel

Reputation: 119

passing variable into driver.execute_script() in python selenium

i was trying to open link in a new tab. and the method i uses is like below.

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

the above code works but i want to add some variable into it to construct full url like this below.

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

this is what i have tried so far

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

but when it opened it adds other things, which gives me the output as below.

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

it is adding %60%7Bcountry%7D%60/%60%7Bid%7D%60 after https://www.tracksellers.com/sellers

Upvotes: 2

Views: 1365

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

You can parse the string like below :

I have taken num string and name string and parsing with %s

see the below code :

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");''')

Upvotes: 2

Related Questions