P
P
patsanchique2021-03-30 17:46:20
Python
patsanchique, 2021-03-30 17:46:20

How to pass variable to escaped js string?

I'm trying to display an alert with the value of my variable in selenium, but I don't quite understand how to pass it to an escaped string, I will be grateful for any help

value = "bar"
driver.execute_script('''
foo = value;
alert(foo);
''')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
patsanchique, 2021-03-30
@patsanchique

value = 'bar'
driver.execute_script("foo = \'" + value + "\'; alert(foo);");

V
Vindicar, 2021-03-30
@Vindicar

You can use f-strings (a relatively new thing).

f'''
foo = {value};
alert(foo);
'''

If you need support for older versions of python, then use string formatting
'''
foo = {value};
alert(foo);
'''.format(value=value)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question