如何使用selenium / webdriver将文本输入到tinceMCE editior中 [英] How to input text into tinceMCE editior using selenium/webdriver

查看:187
本文介绍了如何使用selenium / webdriver将文本输入到tinceMCE editior中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Selenium / Webdriver 自动将一些文本插入到使用 tinymce

I am trying to automatically insert some text using Selenium/Webdriver into a text box created using tinymce

文本框不是普通的香草文本框,所以关注不工作:

The text box is not a plain vanilla textbox so following is not working:

System.out.println("Finding text input element");
    WebElement element =  inputWebDriver.findElement(By.xpath("//html/body/div/form/div/div/div[2]"));  //not working
    //WebElement element = inputWebDriver.findElement(By.tagName("form"));  // not working
    //WebElement element = inputWebDriver.findElement(By.id("tinymce"));  // not working

    System.out.println("Entering something in text input");
    element.sendKeys("Test text");

就像纯文本框一样正常 https://code.google.com/p/selenium/wiki/GettingStarted

like it is working fine with plain text box https://code.google.com/p/selenium/wiki/GettingStarted

以下是截图如何在浏览器的元素选项卡中看到textarea元素的位置:
http://imageshack.com/a/img812/9341/1zau.png

Here is screenshot how the textarea element's location is seen in browser's element tab: http://imageshack.com/a/img812/9341/1zau.png

注意:通过硒,我我无法获得'嵌入'html文档中的任何元素(我得到元素未找到错误)

Note: Through selenium, I am not able to get any element inside the 'embedded' html doc ( i get element not found error)

我发现python相当于上面做的,但是,仍然希望在我的java代码中完成它:

I have found a python equivalent to get done above, but, still looking to get it done in my java code:

browser.execute_script("tinyMCE.activeEditor.setContent('{}')".format(testTextVar))


推荐答案

有多个这样做的方式。这是一篇你可能想看的文章。

There are multiple ways of doing it. Here's an article you might want to have a look.

使用Selenium WebDriver测试WYSIWYG编辑器

以下代码片段未经过测试,仅提供Java中的逻辑。

Code snippets below are not tested, only provide the logic in Java.


  • 直接发送密钥。与Richard的答案相同上方

  • Send keys directly. Same as Richard's answer above.
inputWebDriver.switchTo().frame("input-data_ifr");
WebElement element = inputWebDriver.findElement(By.cssSelector("body"));
element.sendKeys("Send keys");




  • 设置innerHTML

  • inputWebDriver.switchTo().frame("input-data_ifr");
    WebElement element = inputWebDriver.findElement(By.cssSelector("body"));
    (JavascriptExecutor)driver.executeScript("arguments[0].innerHTML = '<h1>Set text using innerHTML</h1>'", element);
    




    • 使用TinyMCE的原生API

    • // no need to switch iframe
      (JavascriptExecutor)driver.executeScript("tinyMCE.activeEditor.setContent('<h1>Native API text</h1> TinyMCE')");
      

      这篇关于如何使用selenium / webdriver将文本输入到tinceMCE editior中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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