Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SeleniumWebDriver中的JavascriptException_Javascript_Java_Selenium_Arcgis_Esri - Fatal编程技术网

SeleniumWebDriver中的JavascriptException

SeleniumWebDriver中的JavascriptException,javascript,java,selenium,arcgis,esri,Javascript,Java,Selenium,Arcgis,Esri,我尝试在SeleniumWebDriver中执行java脚本。脚本存储在字符串变量中。并将变量传递给exexuteScript()方法。在运行脚本时,我得到了javascriptexception。我执行的代码如下所示 String js="<html>\r\n" + "<head>\r\n" + " <link rel=\"stylesheet\" href=\"https://js.arc

我尝试在SeleniumWebDriver中执行java脚本。脚本存储在字符串变量中。并将变量传递给exexuteScript()方法。在运行脚本时,我得到了javascriptexception。我执行的代码如下所示

String js="<html>\r\n" + 
                "<head>\r\n" + 
                "    <link rel=\"stylesheet\" href=\"https://js.arcgis.com/3.18/dijit/themes/tundra/tundra.css\">\r\n" + 
                "    <link rel=\"stylesheet\" href=\"https://js.arcgis.com/3.18/esri/css/esri.css\"> \r\n" + 
                "   <script src=\"https://js.arcgis.com/3.31/\"></script>\r\n" + 
                "   <script>\r\n" + 
                "   var map;\r\n" + 
                "   var width = 123.26;\r\n" + 
                "   var height = 126.5;\r\n" + 
                "   require([\"esri/map\",\"esri/geometry/Point\", \"esri/SpatialReference\",\"esri/geometry/Extent\",\"esri/geometry/screenUtils\"],function(Map,Point,SpatialReference,Extent,screenUtils){\r\n" + 
                "       map = new Map(\"mapDiv\",{\r\n" + 
                "           basemap: \"streets\",\r\n" + 
                "           center: [-95.249, 38.954],\r\n" + 
                "           zoom: 5,\r\n" + 
                "           slider: false\r\n" + 
                "       });\r\n" + 
                "       //alert(point);\r\n" + 
                "       \r\n" + 
                "       var mappoint=new Point(-118.15, 33.80, new SpatialReference({ wkid: 4326 }));\r\n" + 
                "       console.log(mappoint);\r\n" + 
                "       \r\n" + 
                "       var extent = new esri.geometry.Extent({\r\n" + 
                "           \"xmin\":-122.68,\"ymin\":45.53,\"xmax\":-122.45,\"ymax\":45.6,\r\n" + 
                "           \"spatialReference\":{\"wkid\":4326}\r\n" + 
                "       });\r\n" + 
                "       console.log(extent);\r\n" + 
                "       var sp = esri.geometry.toScreenPoint(extent,width,height,mappoint);\r\n" + 
                "       console.log(sp);\r\n" + 
                "   });\r\n" + 
                "   </script>\r\n" + 
                "   </head>\r\n" + 
                "  <body>\r\n" + 
                "    <div id=\"mapDiv\"></div>\r\n" + 
                "    </div>\r\n" + 
                "  </body>\r\n" + 
                "</html>\r\n" + 
                "\r\n" + 
                "";
         JavascriptExecutor executor = (JavascriptExecutor)driver;
        executor.executeScript(js);
Exception in thread "main" org.openqa.selenium.JavascriptException: javascript error: Unexpected token '<'
  (Session info: chrome=79.0.3945.130)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'DESKTOP-6H32JSB', ip: '10.8.2.62', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_231'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 78.0.3904.105 (60e2d8774a81..., userDataDir: C:\Users\user\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:54082}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 7c8b20fbb56364eb89ed96357cca06c4
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:489)
    at esrimap.ClickOnanyLocation.main(ClickOnanyLocation.java:85)
String js=“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“变量映射;\r\n”+
“变量宽度=123.26;\r\n”+
“变量高度=126.5;\r\n”+
“要求([\“esri/map\”、\“esri/geometry/Point\”、“esri/SpatialReference\”、“esri/geometry/Extent\”、“esri/geometry/screenUtils\”),函数(映射、点、空间引用、Extent、screenUtils){\r\n”+
map=新映射(\'mapDiv\,{\r\n'+
“基本地图:\'streets\',\r\n”+
“中心:[-95.249,38.954],\r\n”+
“缩放:5,\r\n”+
“滑块:错误\r\n”+
“});\r\n”+
//警报(点);\r\n“+
“\r\n”+
“var mappoint=new Point(-118.15,33.80,new SpatialReference({wkid:4326}));\r\n”+
console.log(映射点);\r\n“+
“\r\n”+
“var extent=new esri.geometry.extent({\r\n”+
“\'xmin\':-122.68,\'ymin\':45.53,\'xmax\':-122.45,\'ymax\':45.6\r\n”+
“\'spatialReference\':{\'wkid\':4326}\r\n”+
“});\r\n”+
console.log(区段);\r\n“+
“var sp=esri.geometry.toScreenPoint(范围、宽度、高度、映射点);\r\n”+
console.log(sp);\r\n“+
“});\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
“\r\n”+
"";
JavascriptExecutor executor=(JavascriptExecutor)驱动程序;
遗嘱执行人。遗嘱执行书(js);
下面给出了我收到的错误消息

String js="<html>\r\n" + 
                "<head>\r\n" + 
                "    <link rel=\"stylesheet\" href=\"https://js.arcgis.com/3.18/dijit/themes/tundra/tundra.css\">\r\n" + 
                "    <link rel=\"stylesheet\" href=\"https://js.arcgis.com/3.18/esri/css/esri.css\"> \r\n" + 
                "   <script src=\"https://js.arcgis.com/3.31/\"></script>\r\n" + 
                "   <script>\r\n" + 
                "   var map;\r\n" + 
                "   var width = 123.26;\r\n" + 
                "   var height = 126.5;\r\n" + 
                "   require([\"esri/map\",\"esri/geometry/Point\", \"esri/SpatialReference\",\"esri/geometry/Extent\",\"esri/geometry/screenUtils\"],function(Map,Point,SpatialReference,Extent,screenUtils){\r\n" + 
                "       map = new Map(\"mapDiv\",{\r\n" + 
                "           basemap: \"streets\",\r\n" + 
                "           center: [-95.249, 38.954],\r\n" + 
                "           zoom: 5,\r\n" + 
                "           slider: false\r\n" + 
                "       });\r\n" + 
                "       //alert(point);\r\n" + 
                "       \r\n" + 
                "       var mappoint=new Point(-118.15, 33.80, new SpatialReference({ wkid: 4326 }));\r\n" + 
                "       console.log(mappoint);\r\n" + 
                "       \r\n" + 
                "       var extent = new esri.geometry.Extent({\r\n" + 
                "           \"xmin\":-122.68,\"ymin\":45.53,\"xmax\":-122.45,\"ymax\":45.6,\r\n" + 
                "           \"spatialReference\":{\"wkid\":4326}\r\n" + 
                "       });\r\n" + 
                "       console.log(extent);\r\n" + 
                "       var sp = esri.geometry.toScreenPoint(extent,width,height,mappoint);\r\n" + 
                "       console.log(sp);\r\n" + 
                "   });\r\n" + 
                "   </script>\r\n" + 
                "   </head>\r\n" + 
                "  <body>\r\n" + 
                "    <div id=\"mapDiv\"></div>\r\n" + 
                "    </div>\r\n" + 
                "  </body>\r\n" + 
                "</html>\r\n" + 
                "\r\n" + 
                "";
         JavascriptExecutor executor = (JavascriptExecutor)driver;
        executor.executeScript(js);
Exception in thread "main" org.openqa.selenium.JavascriptException: javascript error: Unexpected token '<'
  (Session info: chrome=79.0.3945.130)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'DESKTOP-6H32JSB', ip: '10.8.2.62', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_231'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 78.0.3904.105 (60e2d8774a81..., userDataDir: C:\Users\user\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:54082}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 7c8b20fbb56364eb89ed96357cca06c4
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:489)
    at esrimap.ClickOnanyLocation.main(ClickOnanyLocation.java:85)

线程“main”org.openqa.selenium.JavascriptException中的异常:javascript错误:意外标记“要想使用javascript创建新页面,需要先创建页面(DOM)并传递变量:

String page = "window.open('').document.write(" + js + ")";
然后:

executor.executeScript(page);

为了根据需要使用javascript创建新页面,需要首先创建页面(DOM)并传递变量:

String page = "window.open('').document.write(" + js + ")";
然后:

executor.executeScript(page);

如果替换字符串js=“console.log(‘这是一个js脚本’)”,则会出现此错误,因为您的js字符串不是js脚本,而是带有html标记的字符串;实际上,您将看到它正在执行并打印到控制台。如果您替换字符串js=“console.log('这是一个js脚本')”,您将收到此错误,因为您的js字符串不是js脚本,而是带有html标记的字符串;您将实际看到它被执行并打印到控制台如何将js字符串转换为js脚本。你能帮我吗@ConstantintRepadus将js字符串转换为js脚本是什么意思?如何将js字符串转换为js脚本。你能帮我吗@康斯坦丁:把js字符串转换成js脚本是什么意思?