Question

I have text Editor on a web page, i need to fill its value using selenium scripting in c#. I know how to do it for textbox. i have checked the process from Set value in textbox but when i have tried the same process for text editor, it is not working, i want to get and set the value of editor. please help me how can i do this.

code for getting text of textbox is :

IWebDriver firefoxDriver = new FirefoxDriver();
IWebElement passwordTextBox = Driver.FindElement(By.Id("passwordTextBox"));
passwordTextBox.Clear();
passwordTextBox.SendKeys("password");

I have tried the below code to set value of editor

IWebElement detailFrame = driver.FindElement(By.CssSelector("#cke_1_contents .cke_wysiwyg_frame"));
driver.SwitchTo().Frame(detailFrame);
Thread.Sleep(1000);
var body = driver.FindElement(By.TagName("body")); // then you find the body
Thread.Sleep(1000);
body.SendKeys("<span>hiiiiiiii<span>");
Was it helpful?

Solution 3

I have used the following code to do this:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        js.ExecuteScript("document.getElementsByClassName('cke_wysiwyg_frame')[0].contentDocument.getElementsByClassName('cke_editable_themed')[0].innerHTML='dfjkbgdk';");

that's work for me.....

OTHER TIPS

Looks like you are trying send keys to CKEditor.

Please read through this article: Test WYSIWYG editors using Selenium WebDriver

  • Send keys directly

This approach is the one you have tried and didn't work. It's known to have issues with Firefox. Your code should work for PhantomJS or Chrome. Note that <span>hiiiiiiii<span> will result in actual text in the editor, not a span element.

  • Set innerHTML
IWebElement detailFrame = driver.FindElement(By.CssSelector("#cke_1_contents .cke_wysiwyg_frame"));
driver.SwitchTo().Frame(detailFrame);

var body = driver.FindElement(By.TagName("body")); // then you find the body

var executor = driver as IJavaScriptExecutor;
executor.ExecuteScript("arguments[0].innerHTML = '<span>hiiiiiiii<span>'", body);
  • Use CKEditor's native API
var executor = driver as IJavaScriptExecutor;
executor.ExecuteScript("CKEDITOR.instances.ckeditor.setData('<span>hiiiiiiii<span>");
IWebDriver firefoxDriver = new FirefoxDriver();
IWebElement passwordTextBox = Driver.FindElement(By.Id("passwordTextBox"));
passwordTextBox.Clear();
passwordTextBox.SendKeys("password");

In above code change 2nd line to

IWebElement passwordTextBox = firefoxDriver.FindElement(By.Id("passwordTextBox"));

Also check id of element you are searching By.Id("passwordTextBox") is correct other wise use xpath/css

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top