Skip to content

Commit 186eb82

Browse files
unknownunknown
authored andcommitted
Added properties and methods to a WebElement
New: LocationInView, IsPresent, Value, ScrollIntoView and WaitRemoval
1 parent d6597aa commit 186eb82

2 files changed

Lines changed: 72 additions & 5 deletions

File tree

Selenium/ComInterfaces/_WebElement.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ public interface _WebElement {
3636
[DispId(943), Description("Returns the location of the element in the renderable canvas")]
3737
Point Location();
3838

39+
[DispId(944), Description("Gets the location of an element relative to the origin of the view port.")]
40+
Point LocationInView();
41+
3942
[DispId(947), Description("Returns the size of the element")]
4043
Size Size();
4144

45+
[DispId(957), Description("Whether the element is still attached to the DOM.")]
46+
bool IsPresent { get; }
47+
48+
[DispId(965), Description("Returns the value attribute")]
49+
object Value();
50+
4251
#endregion
4352

4453

@@ -83,6 +92,9 @@ public interface _WebElement {
8392
[DispId(949), Description("Submits a form.")]
8493
void Submit();
8594

95+
[DispId(951), Description("Scrolls the current element into the visible area of the browser window.")]
96+
WebElement ScrollIntoView(bool alignTop = false);
97+
8698
#endregion
8799

88100

@@ -211,6 +223,9 @@ public interface _WebElement {
211223
[DispId(1060), Description("Wait for text")]
212224
WebElement WaitText(string pattern, int timeout = -1);
213225

226+
[DispId(1065), Description("Wait for the web element to be removed from the DOM.")]
227+
void WaitRemoval(int timeout = -1);
228+
214229
#endregion
215230

216231

Selenium/Common/WebElement.cs

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public Point Location() {
132132
/// <summary>
133133
/// Gets the location of an element relative to the origin of the view port.
134134
/// </summary>
135-
public Point LocationInViewport() {
135+
public Point LocationInView() {
136136
var dict = (Dictionary)Send(RequestMethod.GET, "/location_in_view");
137137
return new Point(dict);
138138
}
@@ -164,6 +164,20 @@ public bool IsDisplayed {
164164
}
165165
}
166166

167+
/// <summary>
168+
/// Whether the element is present
169+
/// </summary>
170+
public bool IsPresent {
171+
get {
172+
try{
173+
Send(RequestMethod.GET, "/name");
174+
return true;
175+
} catch (Errors.StaleElementReferenceError) {
176+
return false;
177+
}
178+
}
179+
}
180+
167181
/// <summary>
168182
/// Gets the attribute value.
169183
/// </summary>
@@ -184,6 +198,15 @@ public object CssValue(string property) {
184198
return value;
185199
}
186200

201+
/// <summary>
202+
/// Returns the value attribute
203+
/// </summary>
204+
/// <returns></returns>
205+
public object Value() {
206+
var value = Send(RequestMethod.GET, "/attribute/value");
207+
return value;
208+
}
209+
187210
/// <summary>
188211
/// Gets the screenshot of the current element
189212
/// </summary>
@@ -198,11 +221,13 @@ public Image TakeScreenshot(int delayms = 0) {
198221
}
199222

200223
var dict = (Dictionary)_session.javascript.Execute(
201-
"return arguments[0].getBoundingClientRect()", new[] { this }, false);
224+
"return arguments[0].getBoundingClientRect();", new[] { this }, false);
202225

203-
var rect = new System.Drawing.Rectangle(
204-
Convert.ToInt32(dict["left"]), Convert.ToInt32(dict["top"]),
205-
Convert.ToInt32(dict["width"]), Convert.ToInt32(dict["height"]));
226+
int left = Convert.ToInt32(dict["left"]);
227+
int top = Convert.ToInt32(dict["top"]);
228+
int width = Convert.ToInt32(dict["width"]);
229+
int height = Convert.ToInt32(dict["height"]);
230+
var rect = new System.Drawing.Rectangle(left, top, width, height);
206231

207232
using (Image image = (Image)_session.Send(RequestMethod.GET, "/screenshot")) {
208233
var bitmap = image.GetBitmap();
@@ -321,11 +346,13 @@ public void ReleaseKeys(string modifiers) {
321346
_session.keyboard.KeyUp(modifiers);
322347
}
323348

349+
324350
/// <summary>
325351
/// Simulates typing into the element.
326352
/// </summary>
327353
/// <param name="keysOrModifier">Sequence of keys or a modifier key(Control,Shift...) if the sequence is in keysToSendEx</param>
328354
/// <param name="keys">Optional - Sequence of keys if keysToSend contains modifier key(Control,Shift...)</param>
355+
/// <returns></returns>
329356
/// <example>
330357
/// To Send mobile to an element :
331358
/// <code lang="vbs">
@@ -367,6 +394,17 @@ public void Click(string keys = null) {
367394
_session.keyboard.SendKeys(keys);
368395
}
369396

397+
/// <summary>
398+
/// Scrolls the current element into the visible area of the browser window.
399+
/// </summary>
400+
public WebElement ScrollIntoView(bool alignTop = false) {
401+
string script = "arguments[0].scrollIntoView("
402+
+ (alignTop ? "true);" : "false);");
403+
object[] arguments = { this };
404+
_session.javascript.Execute(script, arguments, false);
405+
return this;
406+
}
407+
370408
#endregion
371409

372410

@@ -576,6 +614,14 @@ public WebElement WaitDisplayed(bool displayed = true, int timeout = -1) {
576614
return this;
577615
}
578616

617+
/// <summary>
618+
/// Wait for the web element to be removed from the DOM.
619+
/// </summary>
620+
/// <param name="timeout"></param>
621+
public void WaitRemoval(int timeout = -1) {
622+
_session.SendUntil(timeout, () => this.IsPresent, (r) => r);
623+
}
624+
579625
#endregion
580626

581627

@@ -606,6 +652,12 @@ public TableElement AsTable() {
606652
/// <param name="script">The JavaScript code to execute.</param>
607653
/// <param name="arguments">The arguments to the script.</param>
608654
/// <returns>The value returned by the script.</returns>
655+
/// <example>
656+
/// <code lang="vbs">
657+
/// Set element = driver.FindElementById("id")
658+
/// Debug.Print element.ExecuteScript("return [this.tagName, this.scrollLeft, this.scrollTop];")
659+
/// </code>
660+
/// </example>
609661
public object ExecuteScript(string script, object arguments = null) {
610662
string newscript = "return (function(){" + script + "}).apply(arguments[0],arguments[1]);";
611663
object[] newargs = FormatArguments(this, arguments);

0 commit comments

Comments
 (0)