我有一个网页,我想在上面添加 jQuery 库,代码如下:
var script = document.createElement('script');
script.src ="https://code.jquery.com/jquery-1.11.3.min.js";
document.body.appendChild(script);
此代码是使用 VBS 代码从 UFT 调用的:
script= "var script = document.createElement('script');" &_
"script.src =" & chr(34) & "https://code.jquery.com/jquery-1.11.3.min.js" & chr(34) & ";" &_
"document.body.appendChild(script);"
Browser("myBrowser").Page("myPage").RunScript(script)
当我运行我的 VBS 代码时,抛出一个错误
"Erreur non spécifiée" | in English "unspecified error"
但我无法弄清楚为什么会抛出此错误,因为 VBS 和我的 js 代码似乎都正常...我正在使用 IE 8 企业模式并且在安全选项中启用了脚本。 我对如何解决这个问题没有更多想法...
谢谢,
最佳答案
UFT's online documentation 涵盖了这一点:
When running this method, you should use an
eval()
function or anonymous function to run the script entered for this method. For example, you can use functions like these:"eval(var remove = document.getElementsByTagName('a')[0]; var per = remove.parentNode; per.removeChild(remove););"
OR
"(function(){var remove = document.getElementsByTagName('a')[0]; var per = remove.parentNode; per.removeChild(remove);})();"
*
请查看修改后的代码,该代码可以进行一些更改
- 代码放在 anonymous function and immediately executed (IIFE) 中
我使用单引号而不是
chr(34)
(这没有区别,只是更具可读性)。script = "(function(){" &_ "var script=document.createElement('script');" &_ "script.src='https://code.jquery.com/jquery-1.11.3.min.js';" &_ "document.body.appendChild(script);" &_ "})();"
关于从 VBS 调用的 Javascript 代码在运行时抛出未指定的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159152/