0

C# で Selenium WebDriver を使用しています。一度ホバーしたい RadMenu があります。クリックしたい特定のWeb要素を持つサブメニューを展開する必要があります。JavaScript を使用して要素をクリックする必要がありますが、メニューが展開されないようです。これを行うために使用できる Java スクリプト コマンドはありますか。例えば:

                IJavaScriptExecutor js = ts.getDriver() as IJavaScriptExecutor;
                js.ExecuteScript("arguments[0].style.display='block'",leftPane_Customer);
                js.ExecuteScript("arguments[0].click()", leftPane_Customer);
                js.ExecuteScript("arguments[0].scrollIntoView(true);",leftPane_Customer);

.click() は最初のメニューを強調表示しているように見えますが、それは私が得ることができる限りです。サブメニューを展開するためのソリューション (javascript 構文を含む) を提供できる人はいますか?

ありがとう

4

1 に答える 1

0

次のようにメソッドを使用してホバーイベントをシミュレートできます

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/

于 2017-01-06T20:02:54.443 に答える