2016-12-27 12 views
1

要件:Indeed.comへ QAエンジニアを使用するフィールドでは、シアトル、ワシントン州のどこのフィールドを使用しますか。 Amazonまたはオートメーションを除くすべてのページからジョブの説明/タイトルを印刷非表示のページからの情報の取得

問題:Firepathでこのxpathを使用しています// div [contains(@ id、 'p')] [contains(@ class、 'row' )]は、第1ページのすべての ジョブを選択します。しかし、私が下のコードを実行すると、最初のページ の最初の仕事の説明が何度も何度も印刷されますが、同時に他のページをクリックし続けます。私は取得しています

出力:

SENIOR品質保証ENGINEER- CIVIL ... サウンドトランジット - 12件のレビュー - シアトル、WA 79626 $ - $ 99533年 メンター、コーチ、確立維持に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の内側にある必要があります。あなたがdriverを作成するときに、一度にそれを定義し、注意点として、暗黙の待機driver.manage().timeouts().implicitlyWaitに各反復を定義する必要がこのような何か(テストしていません)

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(); 
} 

を試すことはできません。待機したい場合は、明示的に待ちます。Expected Conditions

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By by)); 
関連する問題