Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get and set text editor value in selenium

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>");
like image 732
Ram Singh Avatar asked May 02 '14 05:05

Ram Singh


People also ask

What is the easiest way to get the value from a text field in Selenium?

We can get the entered text from a textbox in Selenium webdriver. To obtain the value attribute of an element in the html document, we have to use the getAttribute() method. Then the value is passed as a parameter to the method.

How do you get the text value from the drop down in Selenium?

We can get a selected option in a dropdown in Selenium webdriver. The method getFirstSelectedOption() returns the selected option in the dropdown. Once the option is fetched we can apply getText() method to fetch the text.

Is getText () a WebElement method?

The getText() method returns the visible inner text of a web element.


2 Answers

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

like image 37
Deepak Avatar answered Oct 03 '22 01:10

Deepak


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>");
like image 84
Yi Zeng Avatar answered Oct 03 '22 02:10

Yi Zeng