问题
I have been trying to test a tooltip in my web page using Selenium WebDriver with Firefox 19.
I'm basically trying to use mouse actions to hover over the element that has the tooltip attached in order to test that the tooltip is displayed and to hover over another element to test that the tooltip is hidden.
The first operation works fine but when hovering over another element the tooltip remains visible. This issue does not occur when testing the webpage manually.
Has anyone else encountered this issue before? I'm using Ubuntu 12.04.
回答1:
It seems that the Advanced Actions API relies on native events, which are disabled in the Linux version of Firefox by default. Therefore, they must be enabled in the WebDriver instance explicitly.
FirefoxProfile profile = new FirefoxProfile();
//explicitly enable native events(this is mandatory on Linux system, since they
//are not enabled by default
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
Also, in my case I needed to upgrade the WebDriver to version 2.31 since the hover(moveToElement
) action did not work properly on 2.30 even with native events explicitly enabled. Tested this with version 2.31 of WebDriver and versions 17 and 19 of Firefox on Linux.
For more information you may check this link:
http://code.google.com/p/selenium/wiki/AdvancedUserInteractions#Native_events_versus_synthetic_events
回答2:
I also run into this problem with Selenium 2.30 on Firefox 19. It works fine on FF 18.2.
回答3:
This is a simple but handy method with a javascript call that will send a mouseout() event to whichever element you specify (I prefer to pass them using By but you can change this to whatever you like.
I had a problem with Chrome where tooltips were refusing to close once clicked and obscured other nearby click events causing them to fail. This method saved the day in that case. Hope it helps someone else!
/**
* We need this to close help text after selenium clicks
* (otherwise they hang around and block other events)
*
* @param by
* @throws Exception
*/
public void javascript_mouseout(By by) throws Exception {
for (int i=0; i<10; i++) {
try {
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement element = driver.findElement(by);
js.executeScript("$(arguments[0]).mouseout();", element);
return;
} catch (StaleElementReferenceException e) {
// just catch and continue
} catch (NoSuchElementException e1) {
// just catch and continue
}
}
}
You can call it after any sort of click() event like this:
By by_analysesButton = By.cssSelector("[data-section='Analyses']");
javascript_mouseout(by_analysesButton);
Fyi, mine tries 10x via the for loop with the try/catches because our app has a tendency with Chrome towards stale element exceptions, so if you don't have this problem the method can be pared down considerably.
回答4:
I had the same problem.
At first I used the method moveToElement()
without perform()
.
Then I added Firefox Profile
with setEnableNativeEvents
, but it still didn't work for me.
Finally I solved this issue in this way (simply by adding perform()
:
WebElement username = driver.findElement(By.id("username")); Actions actions = new Actions(driver); actions.moveToElement(username).perform(); WebElement tooltip = driver.findElement(By.id("tooltip")); tooltip.isDisplayed();
and it works fine.
来源:https://stackoverflow.com/questions/15073351/selenium-webdriver-mouse-actions-movetoelement-doesnt-raise-mouseout-event-on-f