[英]Javascript expanding a RadMenu Webelement Selenium Webdriver C#
我在C#中使用Selenium WebDriver。 我有一個RadMenu,一旦我想將其懸停在上面; 它應該展開一個子菜單,其中包含我要單擊的特定Webelement。 我必須使用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語法)?
謝謝
您可以使用以下方法模擬懸停事件
public static void HoverOn(this RemoteWebDriver driver, IWebElement elementToHover)
{
var action = new Actions(driver);
action.MoveToElement(elementToHover).Perform();
}
但是,動態切換元素上的click事件可能會引起很多麻煩。 為了獲得對點擊事件的非常穩定的模擬,我使用以下代碼
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);
}
此代碼是可維護硒項目的一部分。 您可以查看項目站點以獲取有關使用Selenium創建可維護的UI測試的更多信息https://github.com/cezarypiatek/MaintainableSelenium/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.