我正在尝试使用 Selenium/Webdriver 自动插入一些文本进入使用 tinymce 创建的文本框
该文本框不是普通的普通文本框,因此以下内容不起作用:
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
以下是如何在浏览器的元素选项卡中看到文本区域元素的位置的屏幕截图: http://imageshack.com/a/img812/9341/1zau.png
注意:通过selenium,我无法获取“嵌入”html 文档中的任何元素(我得到元素未找到错误)
我已经找到了一个相当于上面 get done 的 python,但是,仍然希望在我的 java 代码中完成它:
browser.execute_script("tinyMCE.activeEditor.setContent('{}')".format(testTextVar))
最佳答案
有多种方法可以做到这一点。这是您可能想看的一篇文章。
Test WYSIWYG editors using Selenium WebDriver
下面的代码片段未经测试,仅提供Java中的逻辑。
- 直接发送 key 。与理查德的回答相同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')");
关于java - 如何使用 selenium/webdriver 将文本输入到tinyMCE编辑器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21684236/