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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
backtrack
  • 7,996
  • 5
  • 52
  • 99

2 Answers2

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.

pguardiario
  • 53,827
  • 19
  • 119
  • 159
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) +"');");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352