【问题标题】:How to Enter text in rich text editor in selenium webdriver?如何在 selenium webdriver 的富文本编辑器中输入文本?
【发布时间】:2017-02-22 17:28:02
【问题描述】:

我们的应用程序中有一个富文本编辑器,我们正在使用 selenium 对其进行自动化。以下是相同的 html。

<iframe style="height: 76px; width: 1004px;"></iframe>
<html><head></head><body spellcheck="false"></body></html>
<head></head>
<body spellcheck="false"></body>
<html><head></head><body spellcheck="false"></body></html>
<iframe style="height: 76px; width: 1004px;"></iframe>
<div class=""><iframe style="height: 76px; width: 1004px;"></iframe></div>
<textarea class="form-control Editor" name="actionUpdate" id="actionUpdateId" style="display: none;"></textarea>

我尝试了多种选择。以下代码在 chrome 浏览器上运行良好

driver.switchTo().frame(0);
WebElement el  =  driver.switchTo().activeElement();
new Actions(driver).moveToElement(el).perform();
driver.findElement(By.xpath("/html/body")).sendKeys("Check");

但是它在 IE11 浏览器上不起作用,因为它无法使用 xpath 找到元素。 两种浏览器之间的区别在于,当我使用 IE 在文本字段中输入内容时,它会转到 textarea 标签。但是在 chrome 中,它输入 body tag 。我曾尝试在 IE 中使用 ID="actionUpdateID" 查找元素,但它会抛出一个异常,提示元素未显示,可能是因为 style="display : none;"

【问题讨论】:

  • 这真的是完整的XPath吗?这对我来说似乎不对。
  • 是的,这就是完整的 xpath,因为我正在切换到 iframe,也许这就是为什么它可以正常使用 /html/body

标签: internet-explorer selenium iframe selenium-webdriver textarea


【解决方案1】:

由于在应用程序中手动输入文本会进入 textarea 元素,因此您可以尝试使用 sendKeys 设置值到 selenium 中的 textarea 元素.
当您使用 seleniumtextarea 元素中设置一些值时,相同的值将被添加到 Web 应用程序中的 富文本编辑器

但是根据您的 HTML 代码,textarea 元素的样式为 display:none,因此您的 selenium 代码将抛出一个 Exception 当你 sendKeys 到元素。如果将 display 属性更改为 inlineblock,则可以解决此问题。

因此,您首先需要将 textarea 的样式设置为 display:inline,然后使用 sendKeys 为元素设置一些值。

使用 JavascriptExecutor 改变 textarea 元素的样式:

((JavascriptExecutor)driver).executeScript("document.getElementsByName('actionUpdate')[0].style.display='inline'");

要为您的富文本编辑器设置一些值,请使用以下代码:

driver.findElement(By.name("actionUpdate")).sendKeys("Show this message in rich text editor");

【讨论】:

    【解决方案2】:

    JavaScriptExecutor 是处理富文本框的更好方法。只需切换到正确的 iframe 并将 innerHtml 设置为该 iframe 的主体即可。

    WebElement text= driver.findElement(By.cssSelector("body")); (JavascriptExecutor)driver.executeScript("arguments[0].innerHTML = 'Set text using innerHTML'", text);

    【讨论】:

    • 无法使用 css 选择器 == body 找到元素
    【解决方案3】:

    感谢 Himanshi 我用你的方法解决了这个问题。这是我的代码。

    driver.execute_script("document.getElementsByName('description')[0].style.display='inline'")
    driver.execute_script("document.getElementsByName('description')[0].style.visibility='visible'")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多