Selenium Webdriver:如何导航到子div并在特定div上移动foucs

时间:2017-06-07 10:57:34

标签: java selenium

目前面临导航到许多div标签的问题。 在我的应用程序中,我有一个网格,用于填充或加载多个div标签中的数据。

在鼠标悬停时,我必须单击添加注释选项,该选项将作为灯箱弹出加载。

请指导我如何选择添加评论选项。

<ItemGroup>
    <Compile Remove="[ClientAppRoot]\node_modules\**" />
    <Content Remove="[ClientAppRoot]\node_modules\**" />
    <EmbeddedResource Remove="[ClientAppRoot]\node_modules\**" />
    <None Remove="[ClientAppRoot]\node_modules\**" />
</ItemGroup>

3 个答案:

答案 0 :(得分:0)

首先,您必须使用Action类将鼠标移动到元素,以便可以看到Comment链接。然后你必须对该元素执行操作。

您可以这样做 -

WebElement elem = driver.findElement(By.xpath("//a[contains(text(), 'add a comment')]"));

Actions action = new Actions(driver);
action.moveToElement(elem).click().build().perform();

希望这会有所帮助。

答案 1 :(得分:0)

将鼠标悬停在父元素上以使注释可见。下面的代码可能会给你一些想法。

    Actions action = new Actions(driver);
    action.moveToElement("//nav[@id='cbp-spmenu-s1']").perform();

    //Now the comment button will be visible
    driver.findElement(By.xpath("//a[contains(text(), 'add a comment')]")).click();

希望这会有所帮助。感谢。

答案 2 :(得分:0)

我使用下面的代码解决了这个问题 WebElement we1 = driver.findElement(By.xpath(“(// a [contains(text(),'add a comment')])[1]”));                     JavascriptExecutor elem =(JavascriptExecutor)驱动程序;                     elem.executeScript(“arguments [0] .click();”,we1);