import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindElementsUsingJS {
static WebDriver driver= new FirefoxDriver();
public static void main(String[] args) {
driver.get("http://scripting.jdpoweronline.com/mrIWeb/mrIWeb.dll?I.Project=T1_QTYPE&i.test=1");
// get HTML of above URL
String scriptContents = "return '<html>'+$( 'html' ).html()+'</html>'";
String contentss = (String) ((JavascriptExecutor) driver).executeScript(scriptContents);
// get DOM obj
String content="return $.parseHTML('"+contentss+"')";
System.out.println(((JavascriptExecutor) driver).executeScript(content));
//System.out.println(contentss);
}
}
嗨,我想做以下事情 1.使用JavascriptExecutor在contentss var中获取HTML文档(正常工作) 2.创建收集的HTML的DOM对象(不工作EXCEPTION)。 3.使用预定义的方法获取DOM的元素。
我遇到了异常
> Exception in thread "main" org.openqa.selenium.WebDriverException: unterminated string literal
Command duration or timeout: 14 milliseconds
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:17:32'
System info: host: 'ATMECSINDT-068', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.8.0-37-generic', java.version: '1.7.0_55'
Session ID: c3857c80-97e6-4955-9323-d1418b237441
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=30.0}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:504)
at publicc.FindElementsUsingJS.main(FindElementsUsingJS.java:27)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: unterminated string literal
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:17:32'
System info: host: 'ATMECSINDT-068', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.8.0-37-generic', java.version: '1.7.0_55'
Driver info: driver.version: unknown
at <anonymous class>.handleEvaluateEvent(http://scripting.jdpoweronline.com/
答案 0 :(得分:1)
您对scriptContents
的字符串文字看起来不对。您在'
之前而不是在+
之后有+
。
这是正确的:
String scriptContents = "'<html>'+$( 'html' ).html()+'<html>'";
希望它有所帮助。