问题
I have a web page where clicking on a button (say EXPAND_CONTEXT_MENU, implemented internally as an href) causes a hidden context menu to expand under it, after which I click on of the menu items in the context menu.
Since Selenium 2 does not allow direct access to hidden elements, I first click on EXPAND_CONTEXT_MENU and then click the desired menu item.
While attempting to test this solution, InternetExplorerDriver displays a peculiar behavior. The first click on EXPAND_CONTEXT_MENU expands the menu alright, but before the second click (for selecting the menu item) can occur, EXPAND_CONTEXT_MENU gets minimized again. This results in an ElementNotVisibleException when the second click occurs.
FirefoxDriver performs this test fine. I am using IE8 and Firefox for testing.
Note - in the actual AUT, just a hover on EXPAND_CONTEXT_MENU expands the context menu.
回答1:
use the mouse functionality to hover the mouse over the relevant element:
Locatable hoverItem = (Locatable) driver.findElement(By.xpath("//foo"));
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());
You will need to import locatable, it's not pulled in by default:
import org.openqa.selenium.internal.Locatable;
回答2:
There is probably an event binding that is not firing correctly. Try firing different events via jquery (assumes that driver is the WebDriver instance and that element is the element you want to fire the event on):
((IJavaScriptExecutor)driver).ExecuteScript(string.Format("$(arguments[0]).trigger('click');", element);
来源:https://stackoverflow.com/questions/5482012/selenium-2-webdriver-implementation-not-handling-expandable-menus-correctly