RE: [webdriver] How to get the WebDriver object that WebElement came from?

2,946 views
Skip to first unread message

Peter Gale

unread,
Jul 26, 2012, 4:07:16 PM7/26/12
to webd...@googlegroups.com
I'm not sure whether there is a way. Unless something like WebElement.super returns the parent driver object from which the current element is derived.

However, do you have multiple WebDriver objects? I there's only one, then just make it a global variable. Or set a global variable that holds the current WebDriver object in use.

Or, you could pass the particular WebDriver object you need in the same set of parameters when passing the element to the other class, which would at least be consistent with the passing of the element.




Date: Thu, 26 Jul 2012 10:15:58 -0700
From: aweso...@gmail.com
To: webd...@googlegroups.com
Subject: [webdriver] How to get the WebDriver object that WebElement came from?

I have a WebDriver object from which I get a WebElement. 

Then this element is passed to another class. Is there a way to retrieve the WebDriver the element came from?

Thanks!

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/NpxCQCByrMEJ.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

darrell

unread,
Jul 27, 2012, 10:42:29 AM7/27/12
to webd...@googlegroups.com
If we are talking about Java then there is a way but it is not guaranteed to continue to work. I always pass around the driver and a By element then do a findElement at the top of the method rather than passing in a WebElement. This gives me access to the driver as well as the WebElement.

If you really want to access the internal reference to the WebDriver you can do so using Reflection and altering the state of the WebElement object:

WebElement we = driver.findElement(By.tagName("div"));
Field f = we.getClass().getDeclaredField("parent");
f.setAccessible(true);
Object o = f.get(we);
WebDriver d = null;
if ( o instanceof WebDriver) { 
    d = (WebDriver)o;
}
 
The getClass will find the class definition for the WebElement. The getDeclaredField will find the parent field. This is a private (protected?) field which holds the WebDriver reference. If you try accessing this through reflection you'll get an access exception. The setAccessible will make the field visible to reflection. Finally the Field.get will get the field as an Object and we cast it to a WebDriver.

Problem with this is there is no guarantee that the WebElement will have a field called parent which holds the WebDriver instance. It is generally bad form to do something like this in an OOD but it works right now.

Darrell

JohnMc

unread,
Jul 29, 2012, 3:45:08 AM7/29/12
to webd...@googlegroups.com
Cast the WebElement to a WrapsDriver and then get the driver.

David

unread,
Jul 29, 2012, 2:08:08 PM7/29/12
to webdriver
Interesting discussion. I was wondering if John and Darrell's comments
in some way can also be adapted for other bindings like .NET and
Python for example.

Furthermore, does the JSONWireProtocol also offer a way to track the
WebDriver session/ID that the WebElement found belongs to? If so, then
theoretically, any binding could support this if it's trackable via
the protocol.

darrell

unread,
Jul 30, 2012, 11:04:35 AM7/30/12
to webd...@googlegroups.com
I see no reason this cannot be added to the other bindings. A quick look at the source code for the python WebElement and I see it appears to store the WebDriver in self._parent. If you expose this variable via a getter, you should be able to determine the WebDriver associated with the WebElement.

The C# bindings actually already have an IWrapsDriver interface.
Reply all
Reply to author
Forward
0 new messages