简体   繁体   English

将鼠标悬停在元素上,然后使用Java等待Selenium WebDriver

[英]Hover over on element and wait with Selenium WebDriver using Java

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. Chrome网络驱动器将鼠标悬停在元素上,并且移动速度太快,无法查看文本。 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?). 我查看了Wait()和直到(),但似乎无法使其正常工作(我认为这是因为我并不是真正在等待代码中的布尔值是真的。除非有人提出建议? )。 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. 干杯。

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. ExpectedConditions类中,您有很多方法。

Here is some info: 这里是一些信息:

Hope you find this useful. 希望您觉得这个有帮助。

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! 干杯!

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. 是的,我认为我们可以插入延迟或使用函数(...)。findElements(...)。size()以获得更好的性能。 If the result of the function is not 0, then we can click or do else to the element. 如果函数的结果不为0,则可以单击或对元素执行其他操作。

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. 根据“ https://code.google.com/p/selenium/wiki/GettingStarted ”和“ WebDriver:检查元素是否存在? ”,我们可以插入延迟并使用该函数确定所需元素的存在。

// 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. 下面是C#中的鼠标悬停代码。

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. 上面的代码基本上使用Actions类的MovToElement函数,然后获取元素location(xpath)并获取其属性(可能类似于(alt,title等))并将其存储在字符串中。 Later this value is compared with the text . 稍后将此值与文本进行比较。 If the boolean value is true then your test is pass. 如果布尔值是true,则您的测试通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM