0

I want to pass python variable in to javascript. The variable value is an element id and so I want to pass this variable having element id to javascript "getElementById"

I tried -

element = 'id_of_element'

js = """
var element = document.getElementById(""" + element + """)
"""

But I get error - WebDriverException: Message: u'element is null'. Please advise. I am newbee to this.

2 Answers 2

1

I found the solution -

You have to use a single quote around around the three double quotes

Like this -

element = 'id_of_element'
js = """
var element = document.getElementById('"""+element+"""')
"""
Sign up to request clarification or add additional context in comments.

Comments

0

you can use format :

element = 'id_of_element'

js = """
var element = document.getElementById("{}")
""".format(element)

1 Comment

It's giving me error - var element = document.getElementById("""{element}"""); ^ SyntaxError: invalid syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.