我试图在Selenium Webdriver中执行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)
请支持我解决这个问题。
最佳答案
为了根据需要使用javascript创建新页面,您需要先创建页面(DOM)并传递变量:
String page = "window.open('').document.write(" + js + ")";
接着 :
executor.executeScript(page);
关于javascript - Selenium WebDriver中的JavascriptException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60224354/