9

EDIT: So I figured out a simple way to hover over the element, but I want to wait for a result to pop up. The Chrome webdriver hovers over the element and moves on too fast for me to be able to see text. How can I get it to stay hovered until the text pops up? I looked at Wait() and until(), but I can't seem to get them to work properly (I assume that's because I'm not really waiting for a boolean to be true in the code. Unless someone has some suggestions?). Here's what I have thus far...

WebDriver driver = getWebDriver();
By by = By.xpath("//*[@pageid='" + menuItem + "']");
Actions action = new Actions(driver);
WebElement elem = driver.findElement(by);
action.moveToElement(elem);
action.perform();

Thanks again everyone!

Cheers.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
dr4g1116
  • 258
  • 1
  • 5
  • 13
  • Are you getting any errors? What happens with your current code? Is hovering it over it so quickly that the pop-up disappears? – so cal cheesehead Apr 30 '13 at 18:25
  • What browser are you using? – acdcjunior Apr 30 '13 at 21:25
  • I'm using the Chrome driver. The error that I'm getting is that the element cannot be found, but I know I have the correct element. The command just times out. I see no popup, unfortunately. – dr4g1116 May 01 '13 at 15:50

4 Answers4

12

You can't rely on sleeps so you should try this:

WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

You have a plenty of methods in the ExpectedConditions class.

Here is some info:

Hope you find this useful.

Arthur
  • 3,717
  • 7
  • 28
  • 44
Carlos
  • 160
  • 5
  • True, but for my application it works well. I could overhaul this in the future. Thank you so much for the input! :) I would upvote this if I could!! – dr4g1116 May 28 '13 at 14:31
  • @Carlos I have the same situation as dr4g1116, my case is hovering to a main menu in Chrome, and it move on too fast for me to click the submenu. I tried "action.moveToElement(mainmenu).perform", and "submenu.waitforpresent().click();". But it does not work for Chrome (it works for Firefox with no problem). Any suggestions? – user1559625 Mar 08 '16 at 01:14
  • @Carlos I have a link at http://stackoverflow.com/questions/35855636/why-hover-to-element-and-click-submenu-does-not-work-when-browser-is-opened-full – user1559625 Mar 08 '16 at 01:15
7

Seems at the point I was at the method just was not waiting long enough for the text to become visible. Adding a simple sleep function to the end of it was exactly what I needed.

@When("^I hover over menu item \"(.*)\"$")
public void I_hover_over_menu_item(String menuItem)
{
    WebDriver driver = getWebDriver();
    By by = By.xpath("//*[@pageid='" + menuItem + "']");
    Actions action = new Actions(driver);
    WebElement elem = driver.findElement(by);
    action.moveToElement(elem);
    action.perform();
    this.sleep(2);
}

public void sleep(int seconds) 
{
    try {
        Thread.sleep(seconds * 1000);
    } catch (InterruptedException e) {

    }
}

Hope that helps others in a similar bind!

Cheers!

dr4g1116
  • 258
  • 1
  • 5
  • 13
2

I also have a problem similar with you.

I have solved it.

Yes, I think we can insert delay or use a function, (...).findElements(...).size() , for a better performance. If the result of the function is not 0, then we can click or do else to the element.

According to "https://code.google.com/p/selenium/wiki/GettingStarted" and "WebDriver: check if an element exists?", we can insert delay and use the function to determine the existence of the element we wanted.

// Sleep until the div we want is visible or 5 seconds is over
    long end = System.currentTimeMillis() + 5000;
    while (System.currentTimeMillis() < end) {
        List<WebElement> elements = driver.findElements(By.id("btn"));

        // If results have been returned, the results are displayed in a drop down.
        if (elements.size() != 0) {
          driver.findElement(By.id("btn")).click(); 
          break;
        }
    }

Wait until the element wanted is shown or time is up~!

Community
  • 1
  • 1
sam
  • 21
  • 3
1

Below is code in C# for Mouse Hover.

Actions mousehover = new Actions(driver);
IWebElement Element_Loc = driver.FindElement(By.XPath("html/body/div[1]/table/tbody/tr/td[2]/div[2]/table[2]"));
mousehover.MoveToElement(Element_Loc).Build().Perform();
string Mouse_Text = driver.FindElement(By.XPath("html/body/div[1]/table/tbody/tr/td[2]/div[2]/table[2]")).GetAttribute("alt");

Boolean booltext = Mouse_Text.Equals("your mousehover text goes here.");
Console.WriteLine(booltext);

if (booltext.Equals(true))
{
    Console.WriteLine("The text is verified and matches expected");
}
else
{
    throw new Exception(" The text does not match the expected");
}

The above code basically uses the function MovToElement of Actions class and then takes the element location(xpath) and gets its attribute which maybe like (alt, title etc) and stores it in a string. Later this value is compared with the text . If the boolean value is true then your test is pass.

Holger
  • 285,553
  • 42
  • 434
  • 765
Deepthi S
  • 11
  • 1