简体   繁体   English

使用Selenium Webdriver加载动态URL

[英]Loading dynamic url using Selenium webdriver

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. 我的内存中有一个html字符串(无法将其写入文件),我想在Selenium远程Webdriver中呈现html字符串并获取屏幕截图。 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 这种方法的问题是,由于换行符或其他一些字符,它中断了Java脚本的执行,并出现以下错误

{"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. 我已经在SO中对此进行了调查 ,但并没有太大帮助。

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. 我的问题是我想在selenium驱动程序中加载html字符串并获取屏幕截图。

Assuming _html is your html string, it should be along the lines of: 假设_html是您的html字符串,则应遵循以下内容:

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

You shouldn't need to escape quotes or newlines. 您不需要转义引号或换行符。

To open in the dynamic URL in the same TAB you can use: 要在同一TAB中打开动态URL ,可以使用:

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: 要在新的TAB中打开动态URL ,可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM