将C#与硒和cleditor一起使用 [英] Using C# with selenium and cleditor

查看:140
本文介绍了将C#与硒和cleditor一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用c#中的硒为cleditor文本字段设置主体,但是此过程似乎非常困难.谁能像我4岁那样解释它?

I want to be able to set the body for a cleditor text field using selenium in c#, but this process seems to be very difficult. Can anyone explain it like i'm 4 years old?

推荐答案

基本的Selenium逻辑是您先找到元素,然后对其执行操作.

The basic Selenium logic is you find the element first, then perform actions to it.

但是,在这种情况下,您遇到以下困难:

However, in this case, you have the following difficulties:

  1. 编辑器位于iframe
  2. iframe没有ID,名称甚至没有有意义的src
  3. 编辑器不是输入或文本区域,而是body本身.
  1. the editor is in an iframe
  2. the iframe doesn't have id, name or even meaningful src
  3. the editor is not an input or textarea, but the body itself.

如何导航到iframe (Arran的链接应该是一个很好的教程)

How to navigate into iframes (Arran's link should be a good tutorial to look at)

使用Firefox + Firebug + Firepath查找iframe.

Use Firefox+Firebug+Firepath to find iframes.

如您所见,页面中有四个iframe,您需要以下方法之一才能切换到编辑器框架,而不是其他框架. ()

As you can see, there are four iframes in the page, you need one of the following methods to switch to the editor frame, not the other frames. (source)

IWebDriver Frame(int frameIndex); // works but not desirable, as you have 4 frames, index might be changing
IWebDriver Frame(string frameName); // not working, your editor doesn't have frameName or id.
IWebDriver Frame(IWebElement frameElement); // the way to go, find frame by xpath or css selector in your case

所以我们有:

IWebElement iframe = driver.FindElement(By.XPath("//iframe[@src='javascript:true;']"));
driver.SwitchTo().Frame(iframe);

如何将密钥发送给编辑器

一旦您的驱动程序位于iframe中,通过Firebug,您就可以看到该编辑器实际上是body,而不是inputtextarea.

Once your driver is inside the iframe, through Firebug, you can see that the editor is actually the body, not input or textarea.

因此,您需要找到body元素,将其清除并发送密钥.请注意,Clear()可能不适用于body元素,因此您需要使用IJavaScriptExecutor或发送Control+a首先选择全部.

So you need to find the body element, clear it and send keys. Note that Clear() may not work on body element, so you need either use IJavaScriptExecutor or send Control+a to select all first.

退出iframe

将一些文本发送到编辑器后,您可以使用driver.SwitchTo().DefaultContent();退出.

After some text has been sent to the editor, you can use driver.SwitchTo().DefaultContent(); to get out.

完整的代码

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace SOTest {
    [TestClass]
    public class TestCLEditor {
        [TestMethod]
        public void TestMethod1() {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://premiumsoftware.net/CLEditor");

            // find frames by src like 'javascript:true;' is really not a good idea, but works in this case
            IWebElement iframe = driver.FindElement(By.XPath("//iframe[@src='javascript:true;']"));
            driver.SwitchTo().Frame(iframe);

            IWebElement body = driver.FindElement(By.TagName("body")); // then you find the body
            body.SendKeys(Keys.Control + "a"); // send 'ctrl+a' to select all
            body.SendKeys("Some text");

            // alternative way to send keys to body
            // IJavaScriptExecutor jsExecutor = driver as IJavaScriptExecutor;
            // jsExecutor.ExecuteScript("var body = document.getElementsByTagName('body')[0]; body.innerHTML = 'Some text';");

            driver.Quit();
        }
    }
}

这篇关于将C#与硒和cleditor一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆