Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium从页面获取正确的计数_Java_Selenium - Fatal编程技术网

Java Selenium从页面获取正确的计数

Java Selenium从页面获取正确的计数,java,selenium,Java,Selenium,这段代码应该做什么:这段代码为一个特定的职务返回2页,如果该职务包含某个单词以及每页有多少个职务,它将打印该职务 问题:第一页有3个匹配的标题,第二页有3个标题。然而,在第二页中,它聚合并打印了6个标题,而不是3个 为此搜索找到的页数2 IT质量测试工程师顾问-自动化#28338 质量保证经理 IT质量测试工程师顾问-自动化#28338 页码是1找到的作业数3 IT质量测试工程师顾问-自动化#28338 质量工程与运营总监-106078 数据质量保证工程师(QA)-曼谷基于角色 页码是2找到的作

这段代码应该做什么:这段代码为一个特定的职务返回2页,如果该职务包含某个单词以及每页有多少个职务,它将打印该职务

问题:第一页有3个匹配的标题,第二页有3个标题。然而,在第二页中,它聚合并打印了6个标题,而不是3个

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

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

代码:

import java.util.List;
导入java.util.concurrent.TimeUnit;
导入org.junit.Test;
导入org.openqa.selenium.By;
导入org.openqa.selenium.JavascriptExecutor;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
公共类QAJob{
@试验
公开招聘{
WebDriver=newfirefoxdriver();
驱动程序。获取(“https://www.indeed.com/jobs?st=&psf=advsrch&fromage=7&as_phr=&jt=all&sort="
+“日期和薪水=&l=Bellevue%2C+WA&as\u和=qa+engineer&as\u非%22++%22=&as\u any=&as\u ttl=”
+“&limit%22++%22=10&as_cmp=&radius=5”);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//关闭弹出窗口
试一试{
driver.findElement(By.id(“主弹出关闭按钮”))。单击();
}
捕获(例外e)
{
}
//计算页数
List search_pages=driver.findElements(By.xpath(//span[contains(@class,'pn')]);
System.out.println(“为此搜索找到的页数”+搜索页数.size());
int job_count=0;
int page_count=0;
while(search_pages.size()!=0){
//获得职称
List job_title=driver.findElements(By.xpath(//a[contains(@data tn element,'jobTitle')]);
//获取工作描述
List job_desc=driver.findElements(By.xpath(//div[contains(@id,'p')]][contains(@class,'row')]);
for(WebElement e:job_title){
字符串str_job_title=e.getText();
如果(str_job_title.包含(“质量”)){
System.out.println(str_job_title);
作业计数++;
}           }
//向下滚动
JavascriptExecutor jse=(JavascriptExecutor)驱动程序;
jse.executeScript(“window.scrollBy(01000)”,“”);
//下面的列表将跟踪下一个链接
List next_page=driver.findElements(By.xpath(//span[contains(@class,'np')]][contains(text(),'next')]);
如果(下一页.size()>0){
findElement(By.xpath(//span[contains(@class,'np')][contains(text(),'Next')])))。单击();
页数++;
System.out.println(“页码为“+页码计数+”找到的作业数“+作业计数”);
}
否则{
page_count++;//使用page count++获取打印的最后一页的计数
System.out.println(“页码为“+页码计数+”找到的作业数“+作业计数”);
System.out.println(“搜索结束”);
打破
}
}
}
}

提前感谢您的时间。

您必须将作业计数放入while循环:

int page_count=0;

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

永远不要默默地吞下异常
catch(Exception e){}
@scarywombat在这种情况下,OP应该使用
findElements
并检查返回数组的大小,而不是捕获异常。
int page_count=0;

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