Selenium - MoveToElement() with transparent proxy

孤人 提交于 2019-12-10 18:25:38

问题


I have element

public ArticlePage()
{
    PageFactory.InitElements(Browser.driver, this)
}

[FindsBy(How = How.Id, Using = "someId")]
private IWebElement btnTitleView { get; set; }

and action

Actions action = new Actions(Browser.driver);
action.MoveToElement(btnTitleView).Perform();

But when i try to run it, i will get error

'System.Reflection.TargetException' Object does not match target type.

I tried to locate this element by Browser.driver.FindElement(By.Id("someId")) and then it is working correctly. So, it is present and displayed.
Is it possible to use transparent proxy to perform Actions? Is there any other way to perform MoveToElement() like action on transparent proxy?


回答1:


In order to unwrap element which using transparent proxy you can use IWrapsElement interface which has WrappedElement property:

action.MoveToElement(((IWrapsElement)btnTitleView).WrappedElement).Build().Perform();

You may also want to have that cast included as extension method of IWebElement object:

public static class IWebElementExtensions
{
    public static IWebElement Unwrap(this IWebElement element)
    {
        return ((IWrapsElement)element).WrappedElement;
    }
}

Then the code of your action might look like this:

Actions action = new Actions(Browser.driver);
action.MoveToElement(btnTitleView.Unwrap()).Build().Perform();

I hope that answer will help you with your problem :)




回答2:


One way how to get around this is to use IList<IWebElement> and than use foreach or LINQ to manipulate the element. So you can use:

[FindsBy(How = How.Id, Using = "someId")]
private IList<IWebElement btnTitleView { get; set; }
...

Actions action = new Actions(Browser.driver);
action.MoveToElement(btnTitleView.First()).Perform();

or

foreach (var element in btnTitleView)
{
   Actions action = new Actions(Browser.driver);
   action.MoveToElement(element).Perform();
}


来源:https://stackoverflow.com/questions/35936035/selenium-movetoelement-with-transparent-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!