简体   繁体   English

无法使用Selenium WebDriver将Keys()发送到TinyMCE

[英]Can't sendKeys() to TinyMCE with Selenium WebDriver

I am using Selenium WebDriver in Eclipse and I am trying to insert text into a tinymce box on a web page. 我在Eclipse中使用Selenium WebDriver,我正在尝试将文本插入到网页上的tinymce框中。 This is the code I am using. 这是我正在使用的代码。

//find tinymce iframe
editorFrame = driver.findElement(By.cssSelector(".col_right iframe"));

//switch to iframe
driver.switchTo().frame(editorFrame);
driver.findElement(By.className("mceContentBody")).sendKeys("YOOOO");
driver.switchTo().defaultContent();

The cursor is blinking inside of the editor, however no text is sent. 光标在编辑器内闪烁,但不发送任何文本。 I have also tried this slight variation without any luck. 我也尝试过这种轻微的变化而没有任何运气。

//find tinymce iframe
editorFrame = driver.findElement(By.cssSelector(".col_right iframe"));  

//switch to iframe
driver.switchTo().frame(editorFrame);
WebElement body = driver.findElement(By.className("mceContentBody"));
body.sendKeys("YOOOO");
driver.switchTo().defaultContent();

Yes, as what Richard says, this is a duplicate of How to input text into tinceMCE editior using selenium/webdriver . 是的,正如理查德所说,这是如何使用selenium / webdriver将文本输入到tinceMCE editior的副本

For your specific code, I'd suggest 对于您的具体代码,我建议

  • Try different locator for mceContentBody , eg use By.cssSelector(".mceContentBody") , By.cssSelector("body") , etc. mceContentBody尝试不同的定位器,例如使用By.cssSelector(".mceContentBody")By.cssSelector("body")等。

  • Click the body first before sending keys. 在发送密钥之前先单击正文。

driver.findElement(By.tagName("body")).click().sendKeys("YOOOO");
  • Set innerHTML 设置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 使用TinyMCE的原生API
// no need to switch iframe
(JavascriptExecutor)driver.executeScript("tinyMCE.activeEditor.setContent('<h1>Native API text</h1> TinyMCE')");

Further reading: Test WYSIWYG editors using Selenium WebDriver 进一步阅读: 使用Selenium WebDriver测试WYSIWYG编辑器

这样做(c#)

yourWebDriver.ExecuteScript(string.Format("tinyMCE.getInstanceById('{0}').setContent('{1}');",yourWebElement.GetAttribute("id"), "hello world"));

Snippet for writing to the tinymce editor: 用于写入tinymce编辑器的代码段:

Note - I will not suggest returning void for writing to editor method but keeping it here for simplicity - 注意 - 我不会建议返回void来写入编辑器方法,但为了简单起见将其保留在这里 -

public void writeTextToEditor(String text) {
   WebElement iframe = 
   driver().findElements(By.tagName(UtilTexts.HTML_ELEMENT_IFRAME)).get(0);

   driver().switchTo().frame(iframe);// something to look for when using timymce
   txt_editorBoxSupportArticle.click(); // if the focus is not on the editor window by default you have to perform click.
   txt_editorBoxSupportArticle.sendKeys(text);

   getDriverForCurrentThread().switchTo().defaultContent();
}

Snippet for reading from the tinymce editor: 从tinymce编辑器中读取的片段:

public String readTextFromEditor() {
    @FindBy(how = How.ID, using = ("tinymce"))
    private WebElement txt_editorBoxSupportArticle;

    WebElement iframe = 
    driver().findElements(By.tagName(UtilTexts.HTML_ELEMENT_IFRAME)).get(0);

    driver().switchTo().frame(iframe);
    String articleDesc = txt_editorBoxSupportArticle.getText();
    getDriverForCurrentThread().switchTo().defaultContent();
    return articleDesc;
}

Code snippets above are tested. 上面的代码片段已经过测试。 Although I will also suggest trying TinyMCE's native API. 虽然我也建议尝试使用TinyMCE的原生API。

Please share your thoughts if you find it useful. 如果您觉得有用,请分享您的想法。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM