获取正确的搜索结果页码-Selenium Java

时间:2019-04-29 21:59:45

标签: java selenium while-loop

我正在尝试获取西雅图的正确页码,即7,然后单击从1到7的每页。我有2个问题,请参见下面带有星号**的部分:

1)我不确定如何编写xpath,以便我得到7而不是11

2)while循环是一个无限循环

public class Job {

    public static WebDriver driver;

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Shahriar\\Downloads\\chromedriver_win32\\chromedriver.exe");
        driver= new ChromeDriver();
        driver.get("https://web.expeditors.com/careers/#/listTop");
        driver.manage().window().maximize();

        //Disable the cookie notification at bottom
        driver.findElement(By.xpath("//a[@id='hs-eu-decline-button']")).click();

        // Find more cities and scroll down
        driver.findElement(By.xpath("//a[@id='moreCity']")).click();
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("window.scrollBy(0,5500)", "");

        //Locate US-Seattle and click and verify
        driver.findElement(By.xpath("//label[@class='ng-binding']//input[@id='us~Seattle']")).click();
        String state=driver.findElement(By.xpath("//a[contains(text(),'United States - Seattle')]")).getText();
        Assert.assertEquals(state, "United States - Seattle");

        **//Number of search pages found should be 7
         List<WebElement> resultPages=driver.findElements(By.xpath("//a[@href='#listTop']"));
        Assert.assertEquals(7, resultPages.size());**

        **//Go to each page by clicking on page number at bottom
        int count=0;
        WebElement next_button=driver.findElement(By.xpath("//a[@ng-click='setCurrent(pagination.current + 1)']"));
        while (resultPages.size()!=0){
            next_button.click();
            count++;
            System.out.println("Current page is "+ count);                  
        }**
        //driver.close();
    }
}

我可以使用一些帮助。预先感谢您的宝贵时间。

2 个答案:

答案 0 :(得分:0)

您的while循环正在计数页面,但条件本身似乎是静态的(没有实际的页面我无法分辨)。您可能想要更多类似的东西

        int count=0;
        WebElement next_button=driver.findElement(By.xpath("//a[@ng-click='setCurrent(pagination.current + 1)']"));
        while (next_button != null){
            next_button.click();
            count++;
            System.out.println("Current page is "+ count); 
            next_button=driver.findElement(By.xpath("//a[@ng-click='setCurrent(pagination.current + 1)']"));                 
        }

对于您的7比11结果页面,同样很难不使用HTML,但是您的XPath搜索是针对By.xpath("//a[@href='#listTop']")的,因此,我将使用listTop检查并查看HTML中的内容。也许其他元素正在以某种方式使用该定位标记?

答案 1 :(得分:0)

这是您需要的逻辑。

//Number of search pages found should be 7
     List<WebElement> resultPages=driver.findElements(By.xpath("//a[@href='#listTop' and @ng-click='setCurrent(pageNumber)']"));
    Assert.assertEquals(7, resultPages.size());

    for (int i = 1; i <= resultPages.size(); i++) {
        driver.findElement(By.xpath("(//a[@href='#listTop' and @ng-click='setCurrent(pageNumber)'])[" + i +"]")).click();
        System.out.println(driver.findElement(By.xpath("(//a[@href='#listTop' and @ng-click='setCurrent(pageNumber)'])[" + i +"]")).getText());
    }