Getting WebDriver from IWebElement

c#, selenium, selenium-webdriver, webdriver

Solution

The best way to get the WebDriver from an IWebElement is to distinguish whether the Object type is WebElementProxy or not, like this:

if (this.webElement.GetType().ToString() == 
    "OpenQA.Selenium.Support.PageObjects.WebElementProxy")
{
 this.WebDriver = ((IWrapsDriver)this.webElement
                  .GetType().GetProperty("WrappedElement")
                  .GetValue(this.webElement, null)).WrappedDriver;
}
else
{
  this.WebDriver = ((IWrapsDriver)this.webElement).WrappedDriver;
}

Problem

Is it possible to get WebDriver from IWebElement? I need the following extension: ``` public static bool HasFocus(this IWebElement e) { var driver = ((????)e).WebDriver; var activeElement = driver.SwitchTo().ActiveElement(); return Equals(activeElement, e); } ``` But don't know is it possible to cast the IWebElement to some type to get WebDriver.

Original source