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

我正在尝试获取西雅图的正确页码,即 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();

    }

}

我可以使用一些帮助。在此先感谢您的时间。


缥缈止盈
浏览 104回答 2
2回答

函数式编程

这是您需要的逻辑。//Number of search pages found should be 7&nbsp; &nbsp; &nbsp;List<WebElement> resultPages=driver.findElements(By.xpath("//a[@href='#listTop' and @ng-click='setCurrent(pageNumber)']"));&nbsp; &nbsp; Assert.assertEquals(7, resultPages.size());&nbsp; &nbsp; for (int i = 1; i <= resultPages.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; driver.findElement(By.xpath("(//a[@href='#listTop' and @ng-click='setCurrent(pageNumber)'])[" + i +"]")).click();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(driver.findElement(By.xpath("(//a[@href='#listTop' and @ng-click='setCurrent(pageNumber)'])[" + i +"]")).getText());&nbsp; &nbsp; }

慕码人2483693

您的 while 循环正在计算页面,但条件本身似乎是静态的(如果没有实际页面,我无法判断)。你可能想要更多类似的东西&nbsp; &nbsp; &nbsp; &nbsp; int count=0;&nbsp; &nbsp; &nbsp; &nbsp; WebElement next_button=driver.findElement(By.xpath("//a[@ng-click='setCurrent(pagination.current + 1)']"));&nbsp; &nbsp; &nbsp; &nbsp; while (next_button != null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next_button.click();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Current page is "+ count);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next_button=driver.findElement(By.xpath("//a[@ng-click='setCurrent(pagination.current + 1)']"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }至于您的 7 对 11 结果页面,如果没有 HTML 就很难分辨,但是您的 XPath 搜索是针对的,By.xpath("//a[@href='#listTop']")所以我会检查并查看您的 HTML 中的内容listTop。也许其他元素以某种方式使用该锚标记?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java