有谁知道我如何使用 webdriver 与可爱的编辑器交互。我想清除文字?
<iframe id="CE_Editor1_ID_Frame" src="cuteeditor_files/template.asp"
frameborder="0" class="CuteEditorFrame CuteEditorFrame"
style="background-color: white; border: 1px solid rgb(221, 221, 221);
height: 100%; width: 100%; display: block;"></iframe>
下面的代码不起作用?
driver.switchTo().frame(0);
driver.switchTo().activeElement().clear();
我看了可爱编辑器的演示http://cutesoft.net/example/general.aspx http://cutesoft.net/example/general.aspx,现在我明白你为什么使用driver.switchTo().activeElement()
而不是寻找文本区域/输入,因为 iframe 是“文本区域”,并且您想要清除 iframe 中的所有内容。
我认为你的与演示类似
<iframe id="CE_Editor1_ID_Frame" src="cuteeditor_files/template.asp" frameborder="0" class="CuteEditorFrame CuteEditorFrame" style="background-color: white; border: 1px solid rgb(221, 221, 221); height: 100%; width: 100%; display: block;">
<html>
<head></head>
<body>
<table>the real stuff in the editor, you want to clear this, right?</table>
<br>
<br>
</body>
</html>
</iframe>
我认为 Selenium 没有提供任何删除节点的功能,但是您可以通过 JavascriptExecutor 来实现这一点。Warning:未经测试的代码,这里只有逻辑。你需要自己调试一下。
// first try to avoid switching frames by index, unless you have no other ways.
// if you have only one frame with class name CuteEditorFrame
WebElement editorFrame = driver.findElement(By.cssSelector(".CuteEditorFrame"));
driver.switchTo().frame(editorFrame);
// if the id 'CE_Editor1_ID_Frame' not dynamic
WebElement editorFrame = driver.findElement(By.cssSelector("#CE_Editor1_ID_Frame"));
driver.switchTo().frame(editorFrame); // driver.switchTo().frame("CE_Editor1_ID_Frame");
// then remove everything inside the iframe's body
JavascriptExecutor js;
if (driver instanceof JavascriptExecutor) {
js = (JavascriptExecutor)driver;
}
WebElement editorBody = driver.findElement(By.cssSelector("body"));
js.executeScript("arguments[0].innerHTML = ''", editorBody);
// alternatively, using sendKeys directly is a better way
WebElement body = driver.findElement(By.tagName("body")); // then you find the body
body.sendKeys(Keys.CONTROL + "a"); // send 'ctrl+a' to select all
body.SendKeys("Some text");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)