Frage

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.

War es hilfreich?

Lösung 2

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!

Andere Tipps

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.

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~!

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top