将带有 html/Javascript 的字符串放入 selenium webdriver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22538457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
put a string with html/Javascript into selenium webdriver
提问by luksch
I have a html document in memory as a string. It contains a <script>
tag with a little script that manipulates the dom. I now want to load that html page into selenium webdriver and get back the page after the script manipulates it. Since I have the html already in memory, I don't like the idea much of writing the html into a file and load it as file with driver.get("file://path/to/file")
. So the question is, if there is a possibility to achieve what I want.
我在内存中有一个 html 文档作为字符串。它包含一个<script>
带有操作 dom 的小脚本的标记。我现在想将该 html 页面加载到 selenium webdriver 中,并在脚本对其进行操作后取回该页面。由于我的 html 已经在内存中,我不喜欢将 html 写入文件并将其作为文件加载的想法driver.get("file://path/to/file")
。所以问题是,是否有可能实现我想要的。
IF webdriver can't do it, maybe there is a possibility other than that?
如果 webdriver 不能做到这一点,也许除此之外还有其他可能性?
Here comes an example:
下面是一个例子:
<html><head>
<script type="text/javascript">
function fill(){
var i = "secret"
document.forms[0].elements[1].value=i
}
</script>
</head><body onload="fill()">
<form method="POST"><input type="hidden" name="he1" value="">
<input type="hidden" name="he2" value="">
</form></body></html>
Obviously, I want the webdriver to perform the dom manipulation and change the form according to the script.
显然,我希望webdriver根据脚本执行dom操作并更改表单。
Notethis is just an example. The actual script I need to run does much more complicated things.
请注意,这只是一个示例。我需要运行的实际脚本做了更复杂的事情。
采纳答案by jolancornevin
If you don't want to create a file or load a URL before being able to replace the content of the page, you can always leverage the Data URLsfeature, which supports HTML, CSS and JavaScript:
如果您不想在替换页面内容之前创建文件或加载 URL,您可以随时利用支持 HTML、CSS 和 JavaScript的数据 URL功能:
ChromeDriver driver = new ChromeDriver();
html_content = """
<html>
<head></head>
<body>
<div>
Hello World =)
</div>
</body>
</html>
"""
driver.get("data:text/html;charset=utf-8," + html_content)
回答by lance-java
You could load an empty page eg:
您可以加载一个空页面,例如:
<html></html>
And then set it's innerHTML
然后设置它的innerHTML
ChromeDriver driver = new ChromeDriver();
driver.get("file://empty-page.html");
String innerHtml = "<head>...</head><body onload="...">...</body>";
driver.executeScript("document.innerHTML = " + innerHtml);
Then fire the load event on the body
然后在 body 上触发 load 事件
driver.executeScript("$(document.body).trigger('load');");
Then get the resultant HTML
然后得到结果 HTML
String result = driver.executeScript("document.body.innerHTML;");
回答by lance-java
You could fire up jetty embedded. The jetty instance could then serve in memory html strings as web pages via a Servlet / Handler.
你可以启动jetty embedding。然后,jetty 实例可以通过 Servlet/Handler 在内存中将 html 字符串作为网页提供服务。
回答by FolixOrision
Using Java Selenium 2.4.2 I use the following to replace inner html of an existing element. I use the Apache StringEscapeUtils.escapeJavaScript to escape the HTML because this is a JavaScript replace of the inner html.
使用 Java Selenium 2.4.2 我使用以下内容替换现有元素的内部 html。我使用 Apache StringEscapeUtils.escapeJavaScript 来转义 HTML,因为这是内部 html 的 JavaScript 替换。
public void replaceHTML(By by, String html) {
WebElement e = driver.findElement(by);
((JavascriptExecutor) driver).executeScript("arguments[0].innerHTML='" + StringEscapeUtils.escapeJavaScript(html) + "'", e);
}
Example html String I passed in.
我传入的示例 html 字符串。
<button type="button" onclick="alert('Hello world!')">Click Me!</button>
Notes:
笔记:
I couldn't get "Lance Java's" approach to work because of invalid escaped characters. Adding a single quote after the equal sign fixed this problem.
I tested "Kenneth Baltrinic's" suggestion of using driver.get('about:blank'); but I wasn't able to write to the screen interacting with the base document. In java I had to use double quotes driver.get("about:blank"). I tested this with Chrome.
由于转义字符无效,我无法使用“Lance Java”的方法。在等号后添加单引号解决了这个问题。
我测试了“Kenneth Baltrinic 的”使用 driver.get('about:blank'); 的建议。但我无法写入与基本文档交互的屏幕。在 Java 中,我不得不使用双引号 driver.get("about:blank")。我用 Chrome 测试了这个。
回答by user4883511
Just a small update: escapeEcmaScrip() replaces escapeJavaScript() in 2015.
只是一个小更新:escape EcmaScrip() 在 2015 年取代了 escapeJavaScript()。
public static void main(String[] args) throws InterruptedException{
driver = new FirefoxDriver();
driver.get("http://dhtmlx.com/docs/products/dhtmlxTree/");
replaceHTML(By.xpath("//*/span[text()='Supported browsers:']"), "<button type=\"button\" onclick=\"alert('Hello World!!')\">Click Me!</button>");
}
private static void replaceHTML(By by, String html) {
WebElement e = driver.findElement(by);
((JavascriptExecutor) driver).executeScript("arguments[0].innerHTML='" + StringEscapeUtils.escapeEcmaScript(html) + "'", e);
}