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获取内容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/

最佳答案

您为scriptContents拥有的字符串文字对我来说似乎不对。您在'之前而不是+之后使用+

这是正确的:

String scriptContents = "'<html>'+$( 'html' ).html()+'<html>'";


希望能帮助到你。

09-16 12:59