Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

java - How to input text into tinceMCE editior using selenium/webdriver

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

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

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

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

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Test WYSIWYG editors using Selenium WebDriver

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

  • 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");
  • Set 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);
  • Use TinyMCE's native API
// no need to switch iframe
(JavascriptExecutor)driver.executeScript("tinyMCE.activeEditor.setContent('<h1>Native API text</h1> TinyMCE')");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...