如何在iframe中的Rich Text编辑器中使用SendKeys(webdriver)命令

我面临以下问题。我无法在iframe中输入文本编辑器中的文本:这是html:

     


这是我到目前为止所做的,但测试成功通过,文本编辑器中没有写入任何文本。可能解决方案是使用Javascript执行程序,但我不熟悉它。

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

提前致谢!

有多种方法可以做到这一点。 这是一篇你可能想看的文章。

使用Selenium WebDriver测试WYSIWYG编辑器

  • 直接发送密钥

这种方法是您尝试过但不起作用的方法。 请尝试确保您的定位器正确无误。 否则我建议使用JavaScriptExecutor来获得更稳定的解决方案。

  • 设置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 = '

Set text using innerHTML

'", description);
  • 使用CKEditor的本机API
 // no need to switch iframe (JavascriptExecutor)Browser.instance.executeScript("CKEDITOR.instances.ckeditor.setData('

Native API text

Editor')");