刷新搜索结果后无法在输出中获取正确的文本

时间:2017-08-10 05:53:03

标签: java selenium

这是一个与查找鞋子费用相关的网站。最初我输入了文本并执行了搜索并输出了获得的结果数。

选择条件后,搜索结果将根据新条件进行刷新。 但输出仍然显示以前的结果。我尝试了隐式等待,但它仍然显示旧的结果。

这是代码:

public void openBrowser() 
{
    StaticData.driver.get("http://laceup.io/phase2");
}

public void homeSearchAndClick() 
{
    System.out.println("In home search");
    StaticData.driver.findElement(By.xpath(".//*[@id='search_field']")).sendKeys("Nike");
    StaticData.driver.findElement(By.xpath("html/body/div[1]/div/div[1]/div[1]/div[1]/div/button")).click();
}

// Function to search by FEMALE gender

public void searchForFemale() 
{
    System.out.println("Gender: Female clicked");
    StaticData.driver.findElement(By.xpath("html/body/div[1]/div/section/div[1]/div[1]/div/div[1]/div/div[2]/label/span")).click();
}

public void filterSearchCount() 
{
    StaticData.driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);
    String searchCount = StaticData.driver.findElement(By.xpath(".//*[@id='show_query']/h2")).getText();
    System.out.println("Filter search count: " + searchCount);
}

3 个答案:

答案 0 :(得分:0)

我发现方法driver.navigate().refresh();中使用的filterSearchCount()可以解决问题。即使这样,我也注意到没有检查过滤器的Nike复选框。这样,程序将显示Filter search count: 1919 RESULTS(1918年来自Nike + 1来自Warrior)

如果你想要Nike女人的确切结果,你应该检查文件管理器区域中的Nike,正如我之前告诉过你的那样,即使你在搜索文本框中写了Nike,网站也不会自动检查它

答案 1 :(得分:0)

如果您的目标是获取" Nike"的结果数量。对于女性而言,还有另一种方法可以做到这一点。您可以事先在主页上切换女士们。

enter image description here

仅供参考,我使用PageObjectFactory来简化您的代码

Phase2Page.java - 主页的页面对象工厂

public class Phase2Page {
    WebDriver driver;

    public Phase2Page(WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    @FindBy(xpath = "//*[@id='search_field']")
    WebElement searchField;

    @FindBy (xpath = "html/body/div[1]/div/div[1]/div[1]/div[1]/div/button")
    WebElement searchBtn;

    @FindBy(xpath = ".//*[@id='filter_gender_ul']/li[2]")
    WebElement LadiesToggle;

    public void homeSearchAndClick() {
        System.out.println("In home search");
        LadiesToggle.click();
        searchField.sendKeys("Nike");
        searchBtn.click();
    }
}

SearchPage.java - 搜索页面的页面对象工厂

public class SearchPage {
    WebDriver driver;


    @FindBy(xpath = ".//*[@id='show_query']/h2")
    WebElement searchCount;

    public SearchPage(WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(new AjaxElementLocatorFactory(driver, 50),this);
    }

    @FindBy(xpath = "html/body/div[1]/div/section/div[1]/div[1]/div/div[1]/div/div[2]/label/span")
    WebElement femaleCheckbox;


    // Function to search by FEMALE gender
    public void searchForFemale() {
        System.out.println("Gender: Female clicked");
        femaleCheckbox.click();
    }

    public void filterSearchCount() {
        String count = searchCount.getText();
        System.out.println("Filter search count: " + count);
    }
}

Test1.java - 您的实际执行测试

public class Test1 {
    WebDriver driver;

    public void openBrowser() {
        driver.get("http://laceup.io/phase2");
    }

    @Test
    public void testSearch(){
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium Webdriver/chromedriver.exe");
        driver = new ChromeDriver();
        openBrowser();
        Phase2Page objHome = new Phase2Page(driver);
        SearchPage objSearch = new SearchPage(driver);
        objHome.homeSearchAndClick();
        objSearch.filterSearchCount();
        driver.quit();

    }
}

答案 2 :(得分:0)

当我在filterSearchCount()中使用thread.sleep时,我的代码工作。