如何在 selenium webdriver 的富文本编辑器中输入文本?

How to Enter text in rich text editor in selenium webdriver?

我们的应用程序中有一个富文本编辑器,我们使用 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 标签。我曾尝试在 IE 中使用 ID= "actionUpdateID" 查找元素,但它抛出一个异常,提示未显示元素,可能是因为 style = "display : none;"

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

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

由于在您的应用程序中手动输入文本进入 textarea 元素,您可以尝试使用 sendKeys 将值设置为 textarea selenium 中的元素。
当您使用 seleniumtextarea 元素中设置一些值时,相同的值将添加到 rich text editor 在 Web 应用程序中。

但是根据您的 HTML 代码,textarea 元素的样式设置为 display:none 由于当您向元素 sendKeys 时,您的 selenium 代码将抛出 Exception。如果将 display 属性更改为 inlineblock,则可以解决此问题。

因此,您首先需要将 textarea 的样式设置为 display:inline,然后使用 [= 为元素设置一些值22=]发送密钥。

使用 JavascriptExecutor[= 更改 textarea 元素的样式46=]:

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

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

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

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

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