问题
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