将鼠标悬停在selenium webdriver下拉列表中的某个项目上

时间:2014-09-11 09:44:51

标签: selenium-webdriver

如何将鼠标悬停在selenium webdriver的下拉列表中的某个项目上?我知道点击,但是我需要在每个项目上移动并检查网址是否安全。

由于

3 个答案:

答案 0 :(得分:0)

您可以将鼠标悬停在下拉列表项上,但您可以验证其网址!

您可以通过查看htlm代码来验证它。例如:

<ul id="dropdown">
    <li id="item001" class="">
        <a href="link1.html" title="Link 1">Link 1</a> 
    </li>
    <li id="item002" class="">
        <a href="link2.html" title="Link 2">Link 2</a>    
    </li>

</ul>

在上面的html代码中,我有一个包含2个项目列表1和列表2的下拉列表。

如果要验证每个项目中的链接,您应该从item(属性href)获取链接并将其与预期结果进行比较。 或者即使您想要验证标题,也只需获取标题属性。

希望有用。

答案 1 :(得分:0)

使用Java时,例如:

Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.xpath("xpath")));

答案 2 :(得分:0)

检查此示例我们如何实现此目的。

enter image description here

public class HoverableDropdownTest {

    private WebDriver driver;
    private Actions action;

    Consumer < By > hover = (By by) - > {
        action.moveToElement(driver.findElement(by))
              .perform();
    };

    @Test
    public void hoverTest() {
        driver.get("https://www.bootply.com/render/6FC76YQ4Nh");

        hover.accept(By.linkText("Dropdown"));
        hover.accept(By.linkText("Dropdown Link 5"));
        hover.accept(By.linkText("Dropdown Submenu Link 5.4"));
        hover.accept(By.linkText("Dropdown Submenu Link 5.4.1"));
    }

    @BeforeTest
    public void setupDriver() {
        driver = new FirefoxDriver();
        action = new Actions(driver);
    }

    @AfterTest
    public void teardownDriver() {
        driver.quit();
    }

}

有关详细解答,请点击此处 - http://www.testautomationguru.com/selenium-webdriver-automating-hoverable-multilevel-dropdowns/