【发布时间】:2021-10-02 23:15:16
【问题描述】:
在我的 C# .NET Framework 4.8 项目中,我尝试派生 WindowsElement 类以在新类中实现一些附加功能。 例如 ClickInMiddleOfControl、IsVisible 或 SelectedItem(在 ComboBox 中)。
public class WindowsElementWrapper : WindowsElement
{
public WindowsElementWrapper(RemoteWebDriver parent, string id) : base(parent, id) { }
public void ClickInMiddleOfControl()
{
var rect = this.Rect;
var offsetx = (int)(0.5 * rect.Width);
var offsety = (int)(0.5 * rect.Height);
new Actions(Helper.Session)
.MoveToElement(this, 0, 0)
.MoveByOffset(offsetx, offsety)
.Click()
.Build()
.Perform();
}
}
然后我尝试将 WindowsElement 强制转换为我的 WindowsElementWrapper。
var element = (WindowsElementWrapper)Session.FindElementByName("ElementName");
element.ClickInMiddleOfControl();
但在运行时我收到以下错误消息:
System.InvalidCastException: '无法转换类型的对象 OpenQA.Selenium.Appium.Windows.WindowsElement' 输入 'WEW.WindowsElementWrapper'。'
不能从 WindowsElement 类派生吗?还是我犯了一个根本性的错误?
【问题讨论】: