使用webdriver与可爱的编辑器交互 [英] Interact with a cute editor using webdriver

查看:157
本文介绍了使用webdriver与可爱的编辑器交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何使用webdriver与可爱的编辑器进行交互。我要清除文字?

Does anyone know how I can interact with a cute editor using webdriver. I want to clear the text?

 <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();


推荐答案

我看了一下Cute Editor的演示< a href =http://cutesoft.net/example/general.aspx =nofollow> http://cutesoft.net/example/general.aspx ,现在我明白为什么你用 driver.switchTo()。activeElement()而不是查找textarea / input,因为iframe是'textarea',你想要清除iframe中的所有内容。

I had a look at the Cute Editor's demo at http://cutesoft.net/example/general.aspx, now I understand why you used driver.switchTo().activeElement() instead of looking for textarea/input, because the iframe is the 'textarea', and you want to clear everything within the 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实现此目的。 警告:未经测试的代码,只有逻辑在这里。你需要自己调试一下。

I don't think Selenium provides anything to delete nodes, but you can achieve this by JavascriptExecutor. Warning: untested code, only the logic's here. You need to debug a bit youself.

// 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");

这篇关于使用webdriver与可爱的编辑器交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆