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

Sea*_*ean 5 java wysiwyg selenium tinymce selenium-webdriver

我在Eclipse中使用Selenium WebDriver,我正在尝试将文本插入到网页上的tinymce框中.这是我正在使用的代码.

//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();
Run Code Online (Sandbox Code Playgroud)

光标在编辑器内闪烁,但不发送任何文本.我也尝试过这种轻微的变化而没有任何运气.

//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();
Run Code Online (Sandbox Code Playgroud)

Yi *_*eng 5

是的,正如理查德所说,这是如何使用selenium/webdriver将文本输入到tinceMCE editior的副本.

对于您的具体代码,我建议

  • 尝试不同的定位mceContentBody,例如使用By.cssSelector(".mceContentBody"),By.cssSelector("body")等等.

  • 在发送密钥之前先单击正文.

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

进一步阅读:使用Selenium WebDriver测试WYSIWYG编辑器