2017-10-06 86 views
-2

我試圖從網頁https://iaeme.com/ijmet/index.asp下載所有pdf文件。導航到for循環中的下一個頁面而不會失敗循環?

頁面有不同的鏈接,每個鏈接裏面有多個下載和更多的頁面。我正在嘗試導航下一頁並繼續循環。

package flow; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.URL; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.StandardCopyOption; 
import java.util.List; 
import java.util.NoSuchElementException; 

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import org.apache.tools.ant.taskdefs.Java; 
import org.apache.tools.ant.types.FileList.FileName; 
import org.openqa.selenium.By; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebDriver.Navigation; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.w3c.dom.Text; 

import jxl.common.Assert; 
//kindly ignore the imports 


public class excel { 

    public static void main(String[] args) throws IOException, Exception { 

     System.setProperty("webdriver.chrome.driver", "C:\\Users\\User_2\\Downloads\\chromedriver_win32\\chromedriver.exe"); 
     WebDriver d=new ChromeDriver(); 
     d.manage().window().maximize(); 
     d.get("https://iaeme.com/ijmet/index.asp");     
     java.util.List<WebElement> catvalues=d.findElements(By.className("issue")); 
     for(int i=0;i<=catvalues.size();i++){ 
      catvalues.get(i).click();      
      java.util.List<WebElement> downcount=d.findElements(By.linkText("Download")); 
      System.out.println(downcount.size()); 

      for(int k=1;k<=downcount.size();k++){ 
       downcount.get(k).click();             
       Thread.sleep(5000);       
      } 

      d.navigate().back(); 
      catvalues = d.findElements(By.className("issue")); 
     } 
    } 
} 

我試過了不同的方法失敗了。

回答

1

如果檢查https://iaeme.com/ijmet/index.asp頁面,你可以注意到,與ID 每個類LIK存在的onclick屬性。在此屬性中,您需要打開所有感興趣頁面的信息。

例子:

模式是

onclick="journalpissue('8','9','IJMET')" 

並從此你必須

https://iaeme.com/ijmet/issues.asp?JType=IJMET&VType=8&IType=9

因此,創建這個環節,在這個例子:

由Vtype = 8 ITYPE = 9 =因爲Jtype IJMET

一旦你把所有的鏈接,你可以遍歷所有頁面。

對於每個頁面,您必須獲得所有類別爲id爲jounl的元素的「href」屬性值。

一旦你有了pdf鏈接,我繼續使用「curl」命令。如果你想下載所有與硒的文件,可能是有用的這個答案https://stackoverflow.com/a/37664671/3881320

public class Stackoverflow { 

public static void main(String args[]) { 
     WebDriver driver = new FirefoxDriver(); 
     driver.get("https://iaeme.com/ijmet/index.asp"); 
     java.util.List<WebElement> likValues = driver.findElements(By.className("lik")); 
     LinkedList<String> allUrl = new LinkedList<>(); 
     String baseUrl = "https://iaeme.com/ijmet/"; 
     for (WebElement el : likValues) { 
      String journalpissue = el.getAttribute("onclick"); 
      String relativeUrl = parseJournalpissue(journalpissue); 
      allUrl.add(relativeUrl); 
     } 

     for (String url : allUrl) { 
      analyzePage(driver, baseUrl + url, true); 
     } 

    } 

private static void analyzePage(WebDriver driver, String url, boolean searchOtherPages) { 
     driver.get(url); 
     List<WebElement> allA = null; 
     if (searchOtherPages) { 
      List<WebElement> tdlist = driver.findElements(By.cssSelector("table[class='contant'] tr td")); 
      WebElement pages = tdlist.get(tdlist.size() - 1); 
      System.out.println(pages.getText()); 
      allA = pages.findElements(By.tagName("a")); 
     } 

     java.util.List<WebElement> jounl = driver.findElements(By.className("jounl")); 
     for (WebElement wel : jounl) { 
      String href = wel.getAttribute("href"); 
      if (href.contains(".pdf")) { 
       System.out.println("File to download: " + href); 
       downloadFile(href); 
      } 
     } 

     if (allA != null) { 
      for (WebElement a : allA) { 
       String href = a.getAttribute("href"); 
       System.out.println(href); 
       analyzePage(driver, href, false); 
      } 
     } 
    } 


private static void downloadFile(String file) { 
     try { 
      String[] CMD_COMPOSED = { 
       "/bin/bash", 
       "-c", 
       "curl -O " + file,}; 
      String output; 

      Process p = Runtime.getRuntime().exec(CMD_COMPOSED); 
      StringBuilder outputBuilder; 
      outputBuilder = new StringBuilder(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8)); 
      String line = null; 

      while ((line = reader.readLine()) != null) { 
       outputBuilder.append(line + "\n"); 
      } 
      output = outputBuilder.toString(); 
     } catch (IOException ex) { 
      Logger.getLogger(Stackoverflow.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    private static String parseJournalpissue(String journalpissue) { 
     String finalUrl = null; 

     StringTokenizer st = new StringTokenizer(journalpissue, "'"); 
     st.nextToken(); 
     String vType = st.nextToken(); 
     st.nextToken(); 
     String iType = st.nextToken(); 
     st.nextToken(); 
     String jType = st.nextToken(); 

     finalUrl = "issues.asp?JType=" + jType + "&VType=" + vType + "&IType=" + iType; 
     System.out.println(finalUrl); 
     return finalUrl; 

    } 
} 

注意:我並不認爲在這些頁面(那裏有PDF文件下載)有可能會有更多頁面(您的「更多頁面」,在您的描述中)。爲了做到這一點,你可以使用相同的方法。

編輯:

有關頁數的相關信息是:

enter image description here

即在表類名 「含量的不同」。特別是最後一個因素。

所以:

List<WebElement> tdlist = driver.findElements(By.cssSelector("table[class='contant'] tr td")); 
WebElement pages = tdlist.get(tdlist.size() - 1); 

我們有興趣有關 「一個」 變量名:

List<WebElement> allA = pages.findElements(By.tagName("a")); 

現在我們又有了所有其他網頁的URL。我們可以使用以前的相同方法來下載pdf文件。

+0

謝謝哥們,我會試試這個。 – SarathChandar

+0

好友,我可以下載,但是,我無法導航到下一頁,你能幫我嗎? – SarathChandar

+0

我編輯了答案並插入了允許您在其他頁面上導航的部分。我確信有其他(最佳)方法可以獲得最終目標。我希望我幫助你理解如何思考如何在這類問題中尋找解決方案。 –