0

I have a html string (can not write it to file) in memory, I want to render the html string in Selenium remote webdriver and take the screenshot. Following is the code i used

RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:9515"), capabilities);
driver.get("about:blank");
((JavascriptExecutor) driver)
.executeScript("arguments[0].innerHTML='" + StringEscapeUtils.escapeHtml3(_html) + "'");

The problem with this approach is, it is breaking the java script execution because of the new line character or some other characters and getting the below error

{"errorMessage":"Unexpected EOF","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":

I got the log error message so i have pasted only certain portion of it.

I have looked into this in SO but it did not help me much.

Can you please help me to solve this? My question is i want to load the html string in selenium driver and take the screenshot.

2
  • It seems like saving the html to a file and then opening the file is simplest. Commented Dec 6, 2018 at 23:22
  • i am using Apache strom, and can not save the string to file :( Commented Dec 7, 2018 at 2:35

2 Answers 2

1

Assuming _html is your html string, it should be along the lines of:

driver.executeScript('document.body.innerHTML = arguments[0]', _html)

You shouldn't need to escape quotes or newlines.

Sign up to request clarification or add additional context in comments.

Comments

1

To open in the dynamic URL in the same TAB you can use:

driver.get("about:blank");
((JavascriptExecutor) driver).executeScript("window.location.replace(" + StringEscapeUtils.escapeHtml3(_html) + ");");

To open in the dynamic URL in a new TAB you can use:

driver.get("about:blank");
((JavascriptExecutor) driver).executeScript("window.open('" + StringEscapeUtils.escapeHtml3(_html) +"');");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.