Selenium从页面获取正确的计数

时间:2017-01-30 00:55:34

标签: java selenium

此代码应该做什么:此代码返回特定职位的2个页面,如果该标题包含某个单词以及每页有多少个标题,它将打印职位。

问题:第1页有3个匹配的标题,第2页有3个标题。但是在第二页中,它汇总并打印了6个标题,而不是3个。

为此次搜索找到的页数2 顾问IT质量测试工程师 - 自动化#28338 质量保证经理 顾问IT质量测试工程师 - 自动化#28338 页码是找到的1个工作数量3 顾问IT质量测试工程师 - 自动化#28338 董事 - 质量工程&运营 - 106078 数据质量保证工程师(QA) - 驻曼谷的角色 页码是2个找到的工作数量6 搜索结束

问题:如何使代码打印第2页有3个作业,而不是6个?

代码:

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class QAJob {

    @Test
    public void jobSearch(){
        WebDriver driver= new FirefoxDriver();
        driver.get("https://www.indeed.com/jobs?st=&psf=advsrch&fromage=7&as_phr=&jt=all&sort="
                + "date&salary=&l=Bellevue%2C+WA&as_and=qa+engineer&as_not%22++%22=&as_any=&as_ttl="
                + "&limit%22++%22=10&as_cmp=&radius=5");
        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        //close pop up window

        try{
            driver.findElement(By.id("prime-popover-close-button")).click();
        }
        catch(Exception e)
        {

        }
        //Count number of pages

        List<WebElement> search_pages=driver.findElements(By.xpath("//span[contains(@class,'pn')]"));
        System.out.println("Number of pages found for this search " + search_pages.size());

        int job_count=0;
        int page_count=0;

        while(search_pages.size()!=0){
            //Get job title
            List<WebElement>    job_title=driver.findElements(By.xpath("//a[contains(@data-tn-element,'jobTitle')]"));  

            //Get job description
            List<WebElement> job_desc=driver.findElements(By.xpath("//div [contains(@id,'p')][contains(@class,'row')]"));


            for(WebElement e: job_title){
                String str_job_title= e.getText();
                if(str_job_title.contains("Quality")){
                    System.out.println(str_job_title);
                    job_count++;

                }           }

            //scroll down
            JavascriptExecutor jse = (JavascriptExecutor) driver;
            jse.executeScript("window.scrollBy(0,1000)", "");

            //the following list is keeping track of Next link
            List<WebElement> next_page=driver.findElements(By.xpath("//span[contains(@class,'np')][contains(text(),'Next')]"));
            if(next_page.size()>0){
                driver.findElement(By.xpath("//span[contains(@class,'np')][contains(text(),'Next')]")).click(); 
                page_count++;

                System.out.println("Page number is " + page_count + " number of jobs found " + job_count);
            }

            else{
                page_count++;//using page count++ to get a count number printed of the last page

                System.out.println("Page number is " + page_count + " number of jobs found " + job_count);
                System.out.println("End of search");
                break;
            }

        }
    }
}

提前感谢您的时间。

1 个答案:

答案 0 :(得分:0)

你必须将job_count放入while循环:

int page_count=0;

while(search_pages.size()!=0){
    int job_count=0;