2016-12-27 145 views
1

要求:转到indeed.com 在哪些领域使用QA工程师以及哪些领域使用西雅图,华盛顿州。 打印职位描述/标题从除亚马逊或自动化以外的所有页面从不可见页面获取信息

问题:在Firepath这个xpath我使用// div [contains(@ id,'p')] [contains(@ class,'row' )]选择第1页中的所有 作业。但是,当我执行下面的代码时,它只是一次又一次地打印第一页 的第一份工作描述,同时继续点击其他页面。

输出我得到:

高级质量保证工程师 - 民用设施 酣然的运输 - 12个评论 - 西雅图,华盛顿 $ 79626 - 每年$九九五三三 导师,教练和维持既定的指导QA工程师工作标准。通过对QA提供指导支持QA组内的一致性...... 保存工作 赞助商

代码:

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; 
import org.openqa.selenium.interactions.Actions; 


public class QAJob { 
    int MAX_PAGES; 

    @Test 
    public void jobSearch(){ 
     WebDriver driver= new FirefoxDriver(); 
     driver.get("https://www.indeed.com"); 
     driver.findElement(By.id("what")).sendKeys("QA Engineer"); 
     driver.findElement(By.id("where")).clear(); 
     driver.findElement(By.id("where")).sendKeys("Seattle,WA"); 
     driver.findElement(By.id("fj")).click(); 
     driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 

     // Close the pop up window that appears 
     driver.findElement(By.id("prime-popover-close-button")).click(); 

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

     //Find and print the number of pages found for search  
     List<WebElement> search_pages=driver.findElements(By.xpath("//div[contains(@class,'pagination')]//a")); 
     System.out.println("Number of pages found for job search is " +search_pages.size()); 

     //Code to get and print job descriptions,title 
     List<WebElement> job_desc=driver.findElements(By.xpath("//div[contains(@id,'p')][contains(@class,'row')]")); 

     for(WebElement e: job_desc){ 
      //using String so that I can use 'contains' 
      String str_job_description=e.getText(); 

       while(search_pages.size()!=0){ 

        //find Next link and click on it till the size is !=0 to get to last page 
        driver.findElement(By.xpath("//span[contains(@class,'np')][contains(text(),'Next')]")).click(); 
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
        //Do not want Amazon or Automation jobs 
        if(!((str_job_description.contains("Automation")) || (str_job_description.contains("Amazon")))){ 
         System.out.println(str_job_description); 
        } 

       } 
      } 
     } 
    } 

我可以利用一些有益的建议/意见。在此先感谢您的时间。

回答

0

对于job_desc(外部for环路)中的每个元素,单击下一个按钮,直到到达最后一页(内部while环路)。 for环路应位于while的内部。你可以尝试这样的事情(未测试)

while(search_pages.size() != 0) { 
    List<WebElement> job_desc=driver.findElements(By.xpath("//div[contains(@id,'p')][contains(@class,'row')]")); 
    for(WebElement e: job_desc){ 
     String str_job_description=e.getText(); 
     if(!((str_job_description.contains("Automation")) || (str_job_description.contains("Amazon")))){ 
      System.out.println(str_job_description); 
     } 
    } 
    driver.findElement(By.xpath("//span[contains(@class,'np')][contains(text(),'Next')]")).click(); 
} 

作为一个侧面说明,无需定义隐含等待driver.manage().timeouts().implicitlyWait每次迭代中,当您创建driver一旦定义它。如果你想在那里等待使用Expected Conditions

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By by));