Получение правильных номеров страниц результатов поиска - Selenium Java - PullRequest
0 голосов
/ 30 апреля 2019

Я пытаюсь получить правильный номер страницы для Сиэтла, который равен 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 голосов
/ 30 апреля 2019

Вот логика, которая вам нужна.

//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());
    }
0 голосов
/ 30 апреля 2019

Ваш цикл 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']"), поэтому я проверю и посмотрю, что в вашем HTML с listTop. Возможно, другие элементы каким-то образом используют этот тег привязки?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...