2017-02-02 91 views
1

我試圖在google中輸入selenium,並在記事本文件中獲得結果的所有標題文本。我想獲得所有頁面上的所有可用鏈接,直到搜索的最後一頁。但只有第一頁的鏈接,我越來越。當我調試並運行時,它工作了大約10頁。在此幫助我。無法獲取所有頁面的鏈接

Java代碼:

public class weblink 
{ 
    public static void main(String[] args) throws IOException, InterruptedException { 
    WebDriver driver; 
    System.setProperty("webdriver.chrome.driver", "E:\\disha.shah/myWork/eclipse/chromedriver.exe"); 
    driver = new ChromeDriver(); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    driver.get("http://www.google.co.in/"); 
    driver.findElement(By.id("lst-ib")).sendKeys("Selenium"); 
    driver.findElement(By.id("_fZl")).click(); 

    PrintStream ps = new PrintStream(new File(("E:\\disha1.txt"))); 
    do 
    { 
     List<WebElement> findElements = driver.findElements(By.xpath("//*[@id='rso']//h3/a")); 
     for (WebElement webElement : findElements)  
     { 
      System.out.println("-" + webElement.getText()); // for title 
      //System.out.println(webElement.getAttribute("href")); // for links 
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
      System.setOut(ps); 
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
     } 

     Thread.sleep(1000); 


     if(driver.findElement(By.linkText("Next")).isDisplayed()== true) 
     { 
      driver.findElement(By.linkText("Next")).click();  
     } 
     else 
     { 
      System.out.println("All Link is Covered"); 
     } 


    } 
    while(driver.findElement(By.linkText("Next")).isDisplayed()); 
    { 
     //Thread.sleep(2000); 
    } 


    } 
} 

回答

1

我已經做了一些修正。更新代碼如下.-

public static void main(String[] args) throws IOException, InterruptedException 
{ 
    WebDriver driver; 
    System.setProperty("webdriver.chrome.driver", "D:/Application/chromedriver.exe"); 
    driver = new ChromeDriver(); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
    driver.get("http://www.google.co.in/"); 

    driver.findElement(By.id("lst-ib")).sendKeys("Selenium"); 
    driver.findElement(By.id("_fZl")).click(); 
    Boolean nextButtonFlag = true; 
    // Create two separate file storing the result 
    PrintStream searchTitle = new PrintStream(new File(("D:\\Titles.txt"))); 
    PrintStream searchLink = new PrintStream(new File(("D:\\Links.txt"))); 
    do 
    { 
       List<WebElement> findElements = driver.findElements(By.xpath("//h3[@class='r']/a")); 
       for (WebElement element : findElements) 
       { 
        // Write all received links and title inn txt file 
        searchTitle.append(element.getText()+"\n"); 
        searchLink.append(element.getAttribute("href")+"\n"); 
       } 
        Thread.sleep(2000); 
       try 
       { 
        driver.findElement(By.linkText("Next")).click(); 
       } 
       catch(Exception e) 
       { 
        // no more next button to navigate further link 
        nextButtonFlag=false; 
       } 

       Thread.sleep(2500); 
     } 
     while(nextButtonFlag); 

      System.out.println("Execution done"); 
      searchTitle.close(); 
      searchLink.close(); 
    } 
} 
+0

我已經試過這個問題在我的最後,現在它對我來說工作得很好。 –

+0

其工作,但輸出打印不在新行。它打印在同一行上。 – disha

+0

我可以知道更多關於nextButtonFlag嗎? – disha