如何在iframe中的Rich Text编辑器中使用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编辑器
- 直接发送密钥
这种方法是你尝试过但没有用的方法。请尝试确保您的定位器< iframe>
和< body>
正确无误。否则我建议使用 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中的Rich Text编辑器中使用SendKeys(webdriver)命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!