从 Java Selenium 执行 JavaScript 库和函数

我在 Eclipse 中,编写 Java GUI/脚本,运行无头 chromedriver,我在尝试在驱动程序中加载 html2canvas.js [1] [2]库时遇到问题,然后调用我在其上编写的函数浏览器中的库;我收到此代码的未定义错误:


String ss1ScriptLoc = "C:\\Users\\me\\Desktop\\resources\\html2canvas.min.js";

String ss2ScriptLoc = "C:\\Users\\me\\Desktop\\resources\\takeScreenShot.js";


je.executeScript(

        "var headID1 = document.getElementsByTagName('head')[0]; "

        + "var newScript1 = document.createElement('script'); "

        + "newScript1.type = 'text/javascript'; "

        + "newScript1.src = '" + ss1ScriptLoc + "'; "

        + "headID1.appendChild(newScript1); "

        + "var headID2 = document.getElementsByTagName('head')[0]; "

        + "var newScript2 = document.createElement('script'); "

        + "newScript2.type = 'text/javascript'; "

        + "newScript2.src = '" + ss2ScriptLoc + "'; "

        + "headID2.appendChild(newScript2); "

        + "$(document).ready( function () { takeScreenShot(); });"

    );

这会导致未定义函数“takeScreenShot();”的错误 我以为我已经在本地 .js 文件中定义了它。


Starting ChromeDriver 75.0.3770.140 (-refs/branch-heads/3770@{#1155}) on port 48415

Only local connections are allowed.

Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.

Aug 05, 2019 9:36:53 AM org.openqa.selenium.remote.ProtocolHandshake createSession

INFO: Detected dialect: W3C

java.util.concurrent.ExecutionException: org.openqa.selenium.JavascriptException: javascript error: takeScreenShot is not defined

  (Session info: headless chrome=75.0.3770.142)

Caused by: org.openqa.selenium.JavascriptException: javascript error: takeScreenShot is not defined

  (Session info: headless chrome=75.0.3770.142)

Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'

System info: host: '', ip: '', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'

Driver info: org.openqa.selenium.chrome.ChromeDriver


30秒到达战场
浏览 257回答 2
2回答

跃然一笑

出于安全原因,Chrome 不允许您加载本地资源。您需要将文件放在驱动程序可以访问的地方(网络驱动器/服务器/存储库等),或者读取文件并将其作为参数传递。以下是第二个选项的工作原理:不要src在脚本元素上指定je.executeScript(    $"var headID1 = document.getElementsByTagName('head')[0]; "    + "var newScript1 = document.createElement('script'); "    + "newScript1.type = 'text/javascript'; "    + "var code = {fileText}; "    + "newScript1.appendChild(document.createTextNode(code)); "    + "headID1.appendChild(newScript1); "    + "takeScreenShot();");

largeQ

这是我使用 Selenium 执行/注入本地存储的“.js”文件的方法:&nbsp; File newJSFile = new File(path_to_local_js_file);&nbsp; &nbsp; &nbsp; &nbsp; if (newJSFile.exists())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(new FileInputStream(newJSFile));&nbsp; &nbsp; &nbsp; &nbsp; String js_TxtFile = "";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (sc.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] s = sc.next().split("\r\n");&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < s.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; js_TxtFile += s[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; js_TxtFile += " ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((JavascriptExecutor)driver).executeScript(js_TxtFile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println ("Exception when running Javascript: " + ex.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript