Selenium:元素没有得到子元素

时间:2015-03-17 09:40:47

标签: java selenium xpath selenium-webdriver

我正在做一个selenium自动化测试,我得到一个webelements列表,然后得到每个元素的子元素。但不幸的是,当我这样做时,它只从列表中获取第一个元素的子元素,这很奇怪。任何人都可以指出哪里出错了?感谢。

@Override
public void performTest() {
    performAction();
    try {
        for (WebElement element : search_rows) {
            System.out.println(element.getAttribute("class"));
            System.out.println(element);
            assertElements(element, Constants.Selectors.AMITY_RESULT_ONE);
            assertElements(element, Constants.Selectors.AMITY_RESULT_TWO);
            assertElements(element, Constants.Selectors.AMITY_RESULT_THREE);
        }

        WebElement nextpage = WebDriverUtils.findElementByCssSelector(
                driver, Constants.Selectors.AMITY_NEXT_PAGE);
        WebDriverUtils.scrollToElement(driver, nextpage,
                WebDriverUtils.BY_JAVASCRIPT);
        nextpage.click();
        performTest();
    } catch (Exception e) {
        // e.printStackTrace();
    }
}

@Override
public void performAction() {
    try {
        WebElement searchpage = WebDriverUtils.findElementByXpath(driver,
                Constants.Selectors.AMITY_SEARCHED_ITEMS);
        search_rows = searchpage.findElements(By
                .xpath(Constants.Selectors.AMITY_SEARCH_WRAPPER));
    } catch (Exception e) {
        // e.printStackTrace();
    }
}

private void assertElements(WebElement element, String constant) {
    search_result = element.findElement(By.xpath(constant));
    System.out.println(search_result.getAttribute("class"));
    System.out.println(search_result);

    System.out.println(search_result.findElement(By
            .xpath(Constants.Selectors.AMITY_FIRST_CHOICE)));
    comparetext = search_result.findElement(
            By.xpath(Constants.Selectors.AMITY_FIRST_CHOICE)).getText();
    compareText(combo.getBeds(), comparetext);

    System.out.println(search_result.findElement(By
            .xpath(Constants.Selectors.AMITY_SECOND_CHOICE)));
    comparetext = search_result.findElement(
            By.xpath(Constants.Selectors.AMITY_SECOND_CHOICE)).getText();
    compareText(combo.getBaths(), comparetext);

    System.out.println(search_result.findElement(By
            .xpath(Constants.Selectors.AMITY_THIRD_CHOICE)));
    comparetext = search_result.findElement(
            By.xpath(Constants.Selectors.AMITY_THIRD_CHOICE)).getText();
    compareText(combo.getCarparks(), comparetext);

    System.out.println(search_result.findElement(By
            .xpath(Constants.Selectors.AMITY_NAME)));
    comparetext = search_result.findElement(
            By.xpath(Constants.Selectors.AMITY_NAME)).getText();
    containsText(combo.getSuburb(), comparetext);

    if (combo.getPrice() != null) {
        System.out.println(search_result.findElement(By
                .xpath(Constants.Selectors.AMITY_PRICE)));
        comparetext = search_result.findElement(
                By.xpath(Constants.Selectors.AMITY_PRICE)).getText();
        if (!comparetext.equals("Contact Agent")) {
            comparetext = parsePrice(comparetext);
            comparePrice(combo.getPrice().substring(2), comparetext);
        }
    }
}

以下是常量:

    public static final String AMITY_SEARCHED_ITEMS = "//div[@class='content-sidebar-wrap']";
    public static final String AMITY_SEARCH_WRAPPER = "//div[@class='searches-wrapper']";

    public static final String AMITY_RESULT_ONE = "//div[contains(@class, 'searchresultbox1')]";
    public static final String AMITY_RESULT_TWO = "//div[contains(@class, 'searchresultbox2')]";
    public static final String AMITY_RESULT_THREE = "//div[contains(@class, 'searchresultbox3')]";

    public static final String AMITY_FIRST_CHOICE = "(//div[@class='searchinfo']//span)[1]";
    public static final String AMITY_SECOND_CHOICE = "(//div[@class='searchinfo']//span)[2]";
    public static final String AMITY_THIRD_CHOICE = "(//div[@class='searchinfo']//span)[3]";

    public static final String AMITY_NAME = "//p[@class='p-searchname']";
    public static final String AMITY_PRICE = "//div[@class='search-wrapp']//div[@class='searchprice']//span[@class='spn-search-price']//span[@class='searchprice']";

    public static final String AMITY_NEXT_PAGE = ".pagination-next > a:nth-child(1)";

谢谢。我真的需要快速解决这个问题

2 个答案:

答案 0 :(得分:0)

//开头的XPath是全局的,即使您是从另一个元素的上下文执行它。

尝试使用.//将其限制为元素的子项。

答案 1 :(得分:0)

确保: -

  1. 您已经在search_rows对象中收集了WebElements列表,方法是通过对代码进行删除或在任何Web浏览器的控制台中评估它的选择器

  2. 不会隐藏第一个元素后面的元素。

  3. 如果子元素共享相同的选择器,或者不是迭代WebElements列表,只需在选择器中使用数字谓词。