Skip to content

[πŸ› Bug]: [dotnet] Unable to use wrapped IWebElements from EventFiringWebDriver within ActionsΒ #14513

@ZachBergman

Description

@ZachBergman

What happened?

We are using EventFiringWebDriver, which returns EventFiringWebElements. In our source, we have an IWebElement wrapping class called WebElementWrapper, which implements IWebElement, IWrapsElement, ILocatable. When using our WebElementWrappers within an Action builder, we get the following exception: "Target element cannot be converted to IWebElementReference".

Our setup...

public interface IWebElementWrapper : IWebElement, IWrapsElement, ILocatable
{
    // Excluded for brevity
}

public class WebElementWrapper : IWebElementWrapper
{
    private IWebElement _webElement;
    public IWebElement WebElement
    {
        get
        {
            if (_webElement == null)
            {
                _webElement = GetWebElement();
            }
            return _webElement;
        }
    }

    public IWebElement WrappedElement => WebElement;

    // Excluded irrelevant content for brevity
}

[TestClass]
public class Tests
{
    [TestMethod]
    public void Test()
    {
        // WebDriver is an EventFiringWebDriver
        new Actions(WebDriver)
            .MoveToElement(WebElementWrapperClassInstance)
            .Click()
            .Build()
            .Perform();
    }
}

It seems the cause of this is the implementation here (shown below with my comments)

// OpenQA.Selenium.Interactions.PointerInputDevice.PointerMoveInteraction
private Dictionary<string, object> ConvertElement()
{
    // this.target is an IWebElement, which doesn't necessarily implement IWebDriverObjectReference
    IWebDriverObjectReference elementReference = this.target as IWebDriverObjectReference;
    if (elementReference == null)
    {
        // We are falling to backup IWrapsElement, however IWrapsElement also does not necessarily implement IWebDriverObjectReference
        IWrapsElement elementWrapper = this.target as IWrapsElement;
        if (elementWrapper != null)
        {
            // In our scenario, since our IWebElement originated from EventFiringWebDriver, it is actually an EventFiringWebElement
            // which does not implement IWebDriverObectReference so it ends up being null here.
            elementReference = elementWrapper.WrappedElement as IWebDriverObjectReference;
        }
    }

    if (elementReference == null)
    {
        throw new ArgumentException("Target element cannot be converted to IWebElementReference");
    }

    Dictionary<string, object> elementDictionary = elementReference.ToDictionary();
    return elementDictionary;
}

In combination with the fact that EventFiringWebDriver issues EventFiringWebElements, which do not implement IWebDriverObjectReference like their WebElement cousins
EventFiringWebElement

private class EventFiringWebElement : ITakesScreenshot, IWebElement, IWrapsElement, IWrapsDriver

WebElement

public class WebElement : IWebElement, IFindsElement, IWrapsDriver, ILocatable, ITakesScreenshot, IWebDriverObjectReference

How can we reproduce the issue?

Use the combination of a WebElementWrapper class as shown, use a EventFiringWebDriver, and use the Actions class as shown.

Relevant log output

System.ArgumentException "Target element cannot be converted to IWebElementReference"

Operating System

Windows 10

Selenium version

4.18.0

What are the browser(s) and version(s) where you see this issue?

Any

What are the browser driver(s) and version(s) where you see this issue?

Any

Are you using Selenium Grid?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-dotnet.NET BindingsI-defectSomething is not working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions