如何在 iframe 中的富文本编辑器中使用 SendKeys(webdriver) 命令 [英] How to use SendKeys(webdriver) command in Rich Text editor that is located in iframe
问题描述
我面临以下问题.我无法在带有文本编辑器的 iframe 中输入文本:这是 html:
I'm facing the following problem.I can not type text in iframe in which there is a text editor:Here is the html:
<iframe class="cke_wysiwyg_frame cke_reset" frameborder="0" style="width: 100%; height: 100%;" aria-describedby="cke_39" title="Текстов редактор за форматиран текст,description1" src="" tabindex="0" allowtransparency="true">
<!DOCTYPE html>
<html lang="bg" dir="ltr">
<head>
<body class="cke_editable cke_editable_themed cke_contents_ltr" contenteditable="true" spellcheck="false">
<p>
<br>
</p>
</body>
</html>
</iframe>
这是我到目前为止所做的,但测试成功通过,文本编辑器中没有写入任何文本.可能解决方案是使用 Javascript 执行器,但我不熟悉它.
Here is what I have done so far,but the test passed successfully and no text is writen in the text editor.May be the solution is with Javascript executor but I'm not familiar with it.
WaitTool.waitForElementPresent(Browser.instance, By.tagName("iframe"), 10);
WebElement iframe = Browser.instance.findElement(By.tagName("iframe"));
Browser.instance.switchTo().frame(iframe);
WebElement description=Browser.instance.findElement(By.xpath("//body[@class='cke_editable cke_editable_themed cke_contents_ltr']"));
description.click();
description.sendKeys("someText");
Browser.instance.switchTo().defaultContent();
提前致谢!
推荐答案
有多种方法可以做到.这是一篇您可能想看的文章.
There are multiple ways of doing it. Here's an article you might want to have a look.
使用 Selenium WebDriver 测试 WYSIWYG 编辑器
- 直接发送密钥
这种方法是您尝试过但没有奏效的方法.请尝试确保 和
的定位器是正确的.否则我建议使用
JavaScriptExecutor
以获得更稳定的解决方案.
This approach is the one you have tried and didn't work. Please try make sure your locators to <iframe>
and <body>
are correct. Otherwise I'd suggest using JavaScriptExecutor
for more stable solutions.
- 设置innerHTML
WaitTool.waitForElementPresent(Browser.instance, By.className("cke_wysiwyg_frame"), 10);
WebElement iframe = Browser.instance.findElement(By.className("cke_wysiwyg_frame"));
Browser.instance.switchTo().frame(iframe);
WebElement description = Browser.instance.findElement(By.cssSelector("body"));
(JavascriptExecutor)Browser.instance.executeScript("arguments[0].innerHTML = '<h1>Set text using innerHTML</h1>'", description);
- 使用 CKEditor 的原生 API
// no need to switch iframe
(JavascriptExecutor)Browser.instance.executeScript("CKEDITOR.instances.ckeditor.setData('<h1>Native API text</h1> Editor')");
这篇关于如何在 iframe 中的富文本编辑器中使用 SendKeys(webdriver) 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!