javascript - 使用 Java 从 Alfresco for Selenium WebDriver 的动态制作导航中获取所有元素

标签 javascript java html css selenium

主题 我如何从动态(JS)制作的导航中获取导航元素的所有文本。 我需要它来创建 Selenium WebDriver 脚本 (Java)。

情况 HTML

    <!--//Dynamic generated JS nav menu-->
<div ID="has_special_id_that i_use_to_select"> 

    <!--1 item of the nav menu with no subitem-->
    <div>
        <span>
            <a>I can get this text and echo to Eclipse console</a>
        </span>
    </div>

<!--    // 1 item of the nav menu with subitem-->
<!--    //here there shoulde be also subitems only i can echo a ?-->
    <div>
        <span>Strange item</span>
        <span>
            <span>I can get this text and echo to Eclipse console</span>
        </span>
    </div>
</div>

我能够获取所有元素(父项),但不能获取子项(子项) 也许是 JS 问题,因为它是用 js 制作的动态导航。

我的 Java 代码

package myFirstSeleniumTest;

import java.util.concurrent.TimeUnit;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CheckNavAlf {

    public static void main(String[] args) throws Exception {
        String baseUrl = "https://specifi.url.com";
        WebDriver driver = new FirefoxDriver();
        driver.get(baseUrl);
        driver.findElement(By.name("username")).sendKeys("uname");
        driver.findElement(By.name("password")).sendKeys("pwd");
        driver.findElement(By.name("password")).submit();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        List<WebElement> dropdowns = driver.findElements(By.xpath("//div[@id='uniqName_1_4']/div/span"));
        if (dropdowns.size() >= 1) {

            for (WebElement dropdown : dropdowns) {
//System.out.println(dropdowns.size());
                System.out.println("---------------------------------------");
                System.out.println(dropdown.getText());
                System.out.println("---------------------------------------");
                System.out.println(dropdown.getAttribute("innerHTML"));
                System.out.println("---------------------------------------");
            }
        } else {
            throw new Exception("Error in the code");
        }
//driver.quit();
    }
}

我的结果

Home
---------------------------------------
<a tabindex="-1" title="Home" href="/share/page/site/kennis-databank/wiki-page?title=Main_Page" class="alfresco-navigation-_HtmlAnchorMixin">Home</a>
---------------------------------------
---------------------------------------
My Files
---------------------------------------
<a tabindex="-1" title="My Files" href="/share/page/context/mine/myfiles" class="alfresco-navigation-_HtmlAnchorMixin">My Files</a>
---------------------------------------
---------------------------------------
Shared Files
---------------------------------------
<a tabindex="-1" title="Shared Files" href="/share/page/context/shared/sharedfiles" class="alfresco-navigation-_HtmlAnchorMixin">Shared Files</a>
---------------------------------------
---------------------------------------
Sites
---------------------------------------
<span id="HEADER_SITES_MENU_text" data-dojo-attach-point="containerNode,textDirNode">Sites</span>
---------------------------------------
---------------------------------------
? <= sub menu items, only ? but no content
---------------------------------------
? <= sub menu items, only ? but no content
---------------------------------------
---------------------------------------
Tasks
---------------------------------------
<span id="HEADER_TASKS_text" data-dojo-attach-point="containerNode,textDirNode">Tasks</span>
---------------------------------------
---------------------------------------
? <= <= sub menu items, only ? but no content
---------------------------------------
? <= <= sub menu items, only ? but no content
---------------------------------------
---------------------------------------
People
---------------------------------------
<a tabindex="-1" title="People" href="/share/page/people-finder" class="alfresco-navigation-_HtmlAnchorMixin">People</a>
---------------------------------------

最佳答案

要在测试中打开子菜单,我使用以下方法:

@Override
public Page clickContextMenuSubItem(ActionsMenuItem... items) {

    ActionsMenuItem child = items[0];
    // Hover over every next parent until we're out of parents
    for (ActionsMenuItem nextParent : items) {

        child = nextParent.moveTo();
    }

    child.click();

    LOGGER.log(Level.TRACE, "Context menu sub item clicked");

    return this;
}

我的 ActionsMenuItem 封装了我的 WebElements,但基本上它确实如此

new Actions(driver).moveToElement('My-Element').pause(1).perform();

就我而言,悬停足以打开子菜单。如果您需要在子菜单项打开之前单击它们,则可以使用此选项

new Actions(driver).moveToElement('My-Element').pause(1).click().perform();

这对您有足够的帮助吗?

关于javascript - 使用 Java 从 Alfresco for Selenium WebDriver 的动态制作导航中获取所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37593650/

相关文章:

javascript - 单击两次会产生两个 onClick 事件

javascript - PhantomJS 在第一页跳过标题

javascript - 基于 Angular.js + Node.js/PassportJS 的 SPA 上的 oauth2 身份验证

java - org.springframework.ui.velocity.VelocityEngineFactoryBean 发生了什么?

java - Moxy 条件元素输出

javascript - 给element绑定(bind)onclick调用jquery中的方法

javascript - Angular Material 中更智能的触摸事件

javascript - VBA 到 Java 在 Google 电子表格中转换为 Doubleklick

java - 使用 JSONArray.fromObject 将 Hibernate 实体转换为 json

javascript - 如何在javascript中显示div表单列表