【发布时间】:2021-09-09 09:39:26
【问题描述】:
我正在尝试应用这条线
driver.execute_script("document.querySelector('#StudentImage').setAttribute('src', 'Photos/' + stdCode + '.jpg');")
只有当我直接输入stdCode变量的值时,该行才有效,但是当使用变量stdCode时,我得到了这个错误
JavascriptException: Message: javascript error: stdCode is not defined
(Session info: chrome=91.0.4472.114)
当这样尝试这些行时
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});")
我收到了这个错误
JavascriptException: Message: javascript error: missing ) after argument list
(Session info: chrome=91.0.4472.114)
【问题讨论】:
-
stdCode 是 Python 变量吗?
-
是的,它在 python 代码中并且已经定义。
-
从 Python 的角度来看它看起来不错。也许一些Javascript问题?在这里查看一些信息:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: javascript python selenium