Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll Firefox and IE in WatiN?

Tags:

.net

watin

How to scroll Firefox and IE in WatiN?

like image 976
iJoy Avatar asked May 24 '11 11:05

iJoy


1 Answers

You can call the "scrollIntoView" method for either Internet Explorer or FireFox for any given element using the following code:

For Internet Explorer:

using (var browser = new IE("http://www.google.com"))
{
    var textField = browser.TextField(Find.ByName("q"));
    var nativeElement = textField.NativeElement as IEElement;
    nativeElement.AsHtmlElement.scrollIntoView();
}

For FireFox:

using (var browser = new IE("http://www.google.com"))
{
    var textField = browser.TextField(Find.ByName("q"));
    var nativeElement = textField.NativeElement as JSElement;
    nativeElement.ExecuteMethod("scrollIntoView");
}

Likewise, if you want the position of the element, you can use the same code, but instead of calling scrollIntoView(), you can call offsetLeft() and offsetTop() to get the position.

using (var browser = new IE("http://www.google.com"))
{
    var textField = browser.TextField(Find.ByName("q"))
    var nativeElement = textField.NativeElement as IEElement;

    int leftoffset = nativeElement.AsHtmlElement.offsetLeft();
    int topoffset = nativeElement.AsHtmlelement.offsetTop();
}
like image 138
Nathan Dace Avatar answered Nov 03 '22 01:11

Nathan Dace