0

I am trying to apply this line

driver.execute_script("document.querySelector('#StudentImage').setAttribute('src', 'Photos/' + stdCode + '.jpg');")

The line works only if I entered the value of stdCode variable directly, but when using the variable stdCode, I got this error

JavascriptException: Message: javascript error: stdCode is not defined
  (Session info: chrome=91.0.4472.114)

When trying these lines like that

sPath = 'Photos/' + stdCode + '.jpg'
with open(sPath, 'rb') as f:
    my_string = base64.b64encode(f.read())
    sBase64 = 'data:image/jpeg;base64,' + ''.join(map(chr, my_string))
    print(sBase64)
#driver.execute_script(f"document.querySelector('#StudentImage').setAttribute('src', 'Photos/' + {stdCode} + '.jpg');")
driver.execute_script(f"document.querySelector('#StudentImage').setAttribute('src', {sBase64});")

I got this error

JavascriptException: Message: javascript error: missing ) after argument list
  (Session info: chrome=91.0.4472.114)
3
  • stdCode is a Python variable? Commented Jun 26, 2021 at 8:03
  • Yes it is in the python code and it is defined. Commented Jun 26, 2021 at 8:04
  • It looks fine from Python perspective. Maybe some Javascript issue? Check here for some info: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jun 26, 2021 at 9:22

1 Answer 1

1

Assuming that stdCode is a Python variable, you can use the common way for using variables inside a string:

driver.execute_script(f"document.querySelector('#StudentImage').setAttribute('src', 'Photos/' + {stdCode} + '.jpg');")
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please have a look at the updated question?
Hmmm strange. What if you add one more closing parenthesis at the end of the query like below? {sBase64}))
This doesn't work too. I tried to add apostrophes before and after the variable but this doesn't work too.

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.