How to get and set text editor value in selenium

天涯浪子 提交于 2019-12-10 06:09:53

问题


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>");

回答1:


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>");



回答2:


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




回答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.....



来源:https://stackoverflow.com/questions/23421267/how-to-get-and-set-text-editor-value-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!