Jquery脚本不能正常运行时,通过硒(SCRIPT5009: '$'(未定义).但在Web浏览器控制台

Jquery Scripts not running properly when exected through Selenium(SCRIPT5009: '$' is undefined ) in IE 9. But working properly in Web browser console

本文关键字:未定义 控制台 浏览器 Web 但在 SCRIPT5009 不能 脚本 运行时 Jquery      更新时间:2023-09-26

当使用Selenium时,下面的代码不能正常工作。它在ie9中抛出此错误SCRIPT5009: '$' is undefined。但是当它在Web浏览器控制台中运行时,删除"'"符号后,它工作正常。

String scripts="var script=document.createElement('script');script.onload = function() {alert('"Script loaded and ready'");};script.src = '"http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.js'";document.getElementsByTagName('head')[0].appendChild(script);$('"*'").hide();";
((JavascriptExecutor) driver).executeScript(scripts);

试试这个,

String scripts="var script=document.createElement('script');
  script.onload = function() {alert('Script loaded and ready');};
  script.src = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.js';
  document.getElementsByTagName('head')[0].appendChild(script);
   $('*').hide();";

只需使用'(单引号)而不是"(双引号),因此您不需要"'"