Reputation: 1
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);
}
}
Hi, I want to do following things 1. Get HTML document in contentss var using JavascriptExecutor( working fine) 2. Create DOM object of gathered HTML(Not working EXCEPTION). 3. Get elements of DOM using its predefined methods.
I am getting following exception
> 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/
Upvotes: 0
Views: 1492
Reputation: 13811
The string literal you have for scriptContents
, looks wrong to me. You have the '
before +
rather than after +
.
This is the correct one:
String scriptContents = "'<html>'+$( 'html' ).html()+'<html>'";
Hope it helps.
Upvotes: 1