WatiN控件以及控件的识别方法(一)

WatiN的源代码中可以看到所有的控件识别设计类都是在继承了与element相关的类

包括Element, ElementAttributeBag, ElementCollection, ElementContainer, ElementSupport, ElementTag

首先来看Element的类,此类包含了很多其他类型的DOM控件的模拟方法,包括Button,CheckBox等。可以看到它继承IAttributeBag,Elemental类中写到的方法有:

 1. Classname: Get the name of the stylesheet class assigned to this element;得到传输给此控件默认格式类的名称。返回值为string.

 2. Complete: Boolean Value, Get a value indicating whether the element is completely loaded;得到一个波尔值,查看是否此对当前控件的操作完全加载。返回值为波尔值.

 3. Enabled: Get a Value indicating whether this element is enabled.得到一个波尔值来确认当前的控件是否是可用的.返回值为波尔值.

 4. ID: Get the if of this element as specified in the HTML ,返回当前控件在此HTML页面中的识别ID, 返回值为string

 5.TEXT: Get the inner text of this element(or all the inner text of all the elements contained in this elment). 返回当前控件中的文本,或者是返回当前控件所包含的控件中的所有文本,返回值为string.

 6.TextBefore: Returns the text displayed before this element when it’s wrapped in a Label element, otherwise it returns null. 返回当前控件前面的Label的文本内容,如果当前控件前面的控件不是Label则返回null, 返回值为string.

 7. InnerHtml: Gets the inner HTML of this element. 返回当前控件的内部HTML代码。返回值不用说当然为string.

 8. OuterText: Gets the outer text, 返回外部文档,返回值为string.

    Shit,这个控件我还真不知道怎么用,呵呵。查了一下解释如下:

InnerText:不包括标志,标志以内的纯文本  
  OuterText:
包括标志,连标记及标志内的文本
 
  InnerHtml:
不包括标志,标志以内的Html内容
 
  OuterHtml:
包括标志,连标记及标志内的Html内容

 9. TagName: Gets the tag name of this element 返回当前控件的标签。返回值为string.

 10. Title: Get the title. 返回当前页面的标题。返回值为stieng.

 11. NextSibling: Gets the next sibling of this element in the Dom of the HTML page.返回当前标签在此HTML页面Dom控件中的下一个兄弟控件。返回值为控件即(element)

 12. PreviousSibling: Gets the previous sibling of this element in the Dom of the HTML page. 返回当前标签在此HTML页面Dom控件中的下一个兄弟控件,返回值为控件即(element)

 13. Parent: Gets the parent element of this element, If the parent type is known you can cast it to that type. 返回当前控件的父级控件,如果父级的控件类型知道,可以定义一个和父级同类型的变量控件。返回类型为控件(element).这个比较难理解,下面是举例:

      Div

        a id="watinlink" href="http://watin.sourceforge.net" /

                a href="http://sourceforge.net/projects/watin" /

           Div

      上面的HTML所示第二个超链接没有id,如何点击它来实现超链接?这时可以使用Parent这个方法:

         首先定义一个变量Div,然后把这两个超链接所在的div赋值给这个Division变量。

  Div watinDiv = ie.Link(“watinlink”).Parent;

然后去找第二个Link, 可以把第一个Link看做是Links[0],那么第二个Link就是Link[1]

 watinDiv.Links[1].Click();

 14.Style: Style is the element’s style, 返回当前控件的格式。返回类型为Style.

  15.GetAttributeValue: This method can be used is the attribute is not available as a property of the element of a subclass of the element. Need a parameter’attributeName’, this parameter should be the name of the property exposed by IE on it’s element object. The return is the value of the attribute if available, otherwise null is returned. 当控件的属性不存在或者或者属性属于此控件的一个子类时此方法可以使用。但是需要传递给此方法一个参数即属性名称(它应该是被IE暴露的一个空间实体),返回值为控件的属性值或者为null.

   16. ToString: Returns a string that represents the current object, or null.返回代表当前实体的值,如果没有则返回空。返回类型为string.

   17.Click: Click this element and waits till the event is completely finished(Page is loaded and ready). 点击当前控件然后等待,知道整个事件完成即:页面被完全加载。无返回值.

   18.ClickNoWait: Click this instance and returns immediately. Use this method when you want to continue without waiting for the click event to be finished. Mostly used when a HTML Dialog is displayed after clicking the element. 触发事件并立刻返回信息然后去判断需要如何执行,通常是在点击或者跳转页面还没完成时你需要其它操作。一般情况下是在当弹出对话框时使用这个方法。无返回值。

   19.Focus: Gives the input focus to this element. 聚焦在当前这个输入控件,我不是很明白这个方法。无返回值。

   20.DoubleClick: Doubleclicks this element. 双击当前的控件。无返回值。

   21.KeyDown, KeyPress, KeyUp 三个方法分别为按下键盘不放, 按一下键盘,和松开按键盘。很绕口. 这个应该是从.Net继承的。

   22. Blur: Fires the blur event on this element. 这个应该是和focus相反的方法。取消对这个控件的focus.

23.DOMContainer: Gets the DOMContainer for this element. 返回当前控件的DOM容器。返回值为DomContainer.

   24.HTMLElement: Gets the DOM HTML element for this instance as an object. Cast it to the interface you need. Most of the time the object supports IHTMLElement, IHTMLElement2, IHTMLElement3. You should reference the micorsoft MSHTML.ll assembly to cast it to a valid type. 以一个实例得到当前实例为HTMLDOM控件,并转换为需要的接口.大部分时间应该支持多种转换,需要熟悉MSHTML.dll来转换为合法的类型。返回类型为objectINativeElement.

  25.Exists: Gets a value indicating whether this element exists, true if exists, otherwise false.返得到一个波尔值表明当前的控件是否存在,存在的话是true,否则false. 返回值为波尔值。

  26.WaitUntilExists: Waits until the element exists or will time out after 30 seconds. To change the default time out. 等待直到控件存在,默认30s后超时,可以设置timeout时间。无返回值。

  27.Ancestor: Gets the closest ancestor of the specified AttributConstraint. There is a parameter ‘findBy’ ,Returns an element. If no ancestor of ancestorType is found then null is returned.

得到当前控件最近的一个祖先,需要一个参数FindBy,返回值为控件(Element).

 

 

 

 

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值