次のようにメソッドを使用してホバーイベントをシミュレートできます
public static void HoverOn(this RemoteWebDriver driver, IWebElement elementToHover)
{
var action = new Actions(driver);
action.MoveToElement(elementToHover).Perform();
}
ただし、動的に切り替えられる要素のクリック イベントは、多くの問題を引き起こす可能性があります。クリックイベントの非常に安定したシミュレーションを取得するには、次のコードを使用します
public static void ClickOn(this RemoteWebDriver driver, IWebElement expectedElement)
{
try
{
expectedElement.Click();
}
catch (InvalidOperationException)
{
if (expectedElement.Location.Y > driver.GetWindowHeight())
{
driver.ScrollTo(expectedElement.Location.Y + expectedElement.Size.Height);
Thread.Sleep(500);
}
driver.WaitUntil(SearchElementDefaultTimeout, (d) => driver.IsElementClickable(expectedElement));
expectedElement.Click();
}
}
private static bool IsElementClickable(this RemoteWebDriver driver, IWebElement element)
{
return (bool)driver.ExecuteScript(@"
window.__selenium__isElementClickable = window.__selenium__isElementClickable || function(element)
{
var rec = element.getBoundingClientRect();
var elementAtPosition = document.elementFromPoint(rec.left, rec.top);
return element == elementAtPosition;
};
return window.__selenium__isElementClickable(arguments[0]);
", element);
}
このコードは、Maintainable Selenium プロジェクトの一部です。プロジェクト サイトを確認して、Selenium を使用した保守可能な UI テストの作成に関する詳細情報を取得できますhttps://github.com/cezarypiatek/MaintainableSelenium/