IFrame in webdriver. Typing in RTE

一曲冷凌霜 提交于 2019-12-11 09:56:44

问题


I have a Rich Text Editor on my page, and some text needed to be typed in there. I wrote a solution but it seems to only work intermittently. Here is what I've already tried:

        var iframe = _driver.SwitchTo().Frame(Driver.FindElement(By.XPath(xpath)));
        var editor = iframe.FindElement(By.XPath("//*"));
        for (var i = 1; _driver.FindElement(By.XPath("//*")).Text == String.Empty; i++)
        {
            switch (i)
            {
                case 1:
                    editor.SendKeys(text);
                    break;
                case 2:
                    editor.SendKeys(Keys.Control + "a");
                    editor.SendKeys(Keys.Delete);
                    editor.SendKeys(text);
                    break;
                case 3:
                    editor.Click();
                    editor.SendKeys(text);
                    break;
                case 4:
                    throw new Exception("Rich Text Editor can't be reached");
            }
            editor.SendKeys(text);

In loop I check if text is typed. If it is not I try different cases. Additionally, if I try to execute editor.Clear(); I permanently get an error "Element must be user-editable in order to clear it." I can type (time to time), but cannot clear (permanently). So, the question is how to stabilize this code?


回答1:


Ok, I found the solution. It works through DOM. currentInstance get focused text area, thats why I have click method first.

editor.Click();
IJavaScriptExecutor js = (IJavaScriptExecutor) _driver;
js.ExecuteScript("CKEDITOR.currentInstance.insertText('text')")


来源:https://stackoverflow.com/questions/12508125/iframe-in-webdriver-typing-in-rte

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