尝试调用MoveToElement以便我可以获取特定元素的工具提示。这适用于Chrome。但是,我试图在IE10和Firefox 26.0上做同样的事情并且它确实悬停 - 但只是一瞬间因此没有给我足够的时间来获得工具提示。进入睡眠状态并没有帮助,而且我尽量避免使用thread.sleep。我的问题:是否有另一种方法可以将鼠标悬停在字段或其他一些预期条件上,以便查看工具提示是否出现并保留在Firefox和IE中?
代码段:
/// <summary>
/// Check to see that the hover over option for the 'Defined' column
/// exists and also to return the text for that hover over option.
/// </summary>
/// <returns></returns>
public Tuple<bool, string[]> HoverOverDefinedColumn(bool javascriptWorkaround = false)
{
Thread.Sleep(1000);
var wait = WebDriverWaitObject();
var action = new Actions(driver);
wait.Until(d => HoverOverDefinedRow);
action.MoveToElement(HoverOverDefinedRow).MoveByOffset(5, 0);
action.Build().Perform();
var isThereAnHoverOption = HoverOverOptionExists(wait);
var textDefinedForHoverOption = TextDefined(HoverOptionText);
return new Tuple<bool, string[]>(isThereAnHoverOption, textDefinedForHoverOption);
}
/// <summary>
/// Checks to see specifically if the hover over option exists.
/// </summary>
/// <param name="wait"></param>
/// <returns></returns>
private bool HoverOverOptionExists(WebDriverWait wait)
{
var hoverOverElement =
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("#TTipTDnetst.hintsClass")));
return IsElementPresent(hoverOverElement);
}
/// <summary>
/// Gets the text for the hover over option.
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
private string[] TextDefined(IWebElement element)
{
var path = (element.Text.Split(new string[] { " » " }, StringSplitOptions.None));
return path;
}
答案 0 :(得分:1)
正在测试3种不同的浏览器: (1)Firefox (2)Chrome (3)Internet Explorer
(1)Firefox(v 26.0) 使用Actions类仍然存在'MoveToElement'问题。相反,我执行一个try / catch处理程序,以便在操作不起作用的情况下,异常会运行Javascript方法来获取工具提示,如下所示:
/// <summary>
/// This is the workaround for the hover over functionality
/// for the 'Defined' column. This (for the moment) specifically
/// applies to Firefox and its inability to get the tool tip.
/// </summary>
/// <param name="elemement"></param>
private void HoverOverWorkAround(IWebElement elemement)
{
var code = "var fireOnThis = arguments[0];"
+ "var evObj = document.createEvent('MouseEvents');"
+ "evObj.initEvent( 'mouseover', true, true );"
+ "fireOnThis.dispatchEvent(evObj);";
((IJavaScriptExecutor)driver).ExecuteScript(code, elemement);
}
(2)Chrome(v 32.017) 悬停的动作类与最新的ChromeDriver一样正常。
(3)Internet Explorer 10 只要我有一个带有以下选项的驱动程序,这适用于Actions类:
var options = new InternetExplorerOptions {RequireWindowFocus = true,EnablePersistentHover = false}; instance = new InternetExplorerDriver(ApplicationSettings.DriverLocation,options);
注:
在IE的情况下,默认情况下,“NativeEvents”选项设置为true(对于Windows),我将其留下。
始终使用“本机事件”的含义(在本例中)使用“操作”类来获取悬停功能。如果这不起作用,则捕获异常方法并运行Javascript(如上面的Firefox)。
上述方法的推理来自以下讨论中的想法: