2012-10-21 15 views
10

का उपयोग कर फ़ाइल डाउनलोड करें मैं वेबसाइट के लिए xls फ़ाइल डाउनलोड करने का प्रयास कर रहा हूं। जब मैं फ़ाइल डाउनलोड करने के लिए लिंक पर क्लिक करता हूं, तो मुझे जावास्क्रिप्ट पुष्टिकरण बॉक्स मिलता है। मैं इसेHtmlUnit

ConfirmHandler okHandler = new ConfirmHandler(){ 
      public boolean handleConfirm(Page page, String message) { 
       return true; 
      } 
     }; 
    webClient.setConfirmHandler(okHandler); 

फ़ाइल डाउनलोड करने के लिए एक लिंक है।

<a href="./my_file.php?mode=xls&amp;w=d2hlcmUgc2VsbElkPSd3b3JsZGNvbScgYW5kIHN0YXR1cz0nV0FJVERFTEknIGFuZCBkYXRlIDw9IC0xMzQ4MTUzMjAwICBhbmQgZGF0ZSA%2BPSAtMTM1MDgzMTU5OSA%3D" target="actionFrame" onclick="return confirm('Do you want do download XLS file?')"><u>Download</u></a> 

मैं का उपयोग कर

HTMLPage x = webClient.getPage("http://working.com/download"); 
HtmlAnchor anchor = (HtmlAnchor) x.getFirstByXPath("//a[@target='actionFrame']"); 
anchor.click(); 

handeConfirm() विधि excuted है लिंक पर क्लिक करें। लेकिन मुझे नहीं पता कि सर्वर से फ़ाइल स्ट्रीम को कैसे सहेजना है। मैंने नीचे कोड के साथ स्ट्रीम देखने की कोशिश की।

anchor.click().getWebResponse().getContentAsString(); 

लेकिन, परिणाम पृष्ठ x के समान है। कोई भी सर्वर से स्ट्रीम कैप्चर करने के बारे में जानता है? धन्यवाद।

+0

'anchor.click()' एक पृष्ठ वापस आ जाएगी। इससे आपकी एक्सएलएस फ़ाइल – Lee

+0

पर एक समान प्रश्न का उत्तर देखना चाहिए http://stackoverflow.com/a/28471835/612123 – culmat

उत्तर

7

मुझे वेबविंडो लिस्टनर का उपयोग करके इनपुटस्ट्रीम प्राप्त करने का एक तरीका मिला। WebWindowContentChanged (WebWindowEvent ईवेंट) के अंदर, मैंने नीचे कोड डाला।

InputStream xls = event.getWebWindow().getEnclosedPage().getWebResponse().getContentAsStream(); 

बाद मैं xls मिलता है, मैं अपने हार्ड डिस्क में फ़ाइल को बचा सकता है।

+0

पर मैं एक सीएसवी फ़ाइल डाउनलोड कर रहा हूं, क्या आप यह बता सकते हैं कि ईवेंट क्या है और आप क्लिक कब कॉल कर रहे हैं एंकर पर घटना। मेरे पास फाइल डाउनलोड करने के लिए पुष्टिकरण बॉक्स नहीं है। – Naveen

8

मैंने इसे आपके पोस्ट पर आधारित किया .. नोट: आप केवल विशिष्ट प्रकार की फ़ाइल डाउनलोड करने के लिए सामग्री-प्रकार की स्थिति बदल सकते हैं। उदाहरण के लिए (आवेदन/ऑक्टेट-स्ट्रीम, एप्लिकेशन/पीडीएफ, आदि)।

package net.s4bdigital.export.main; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.List; 

import org.junit.Before; 
import org.junit.Test; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; 

import com.gargoylesoftware.htmlunit.ConfirmHandler; 
import com.gargoylesoftware.htmlunit.Page; 
import com.gargoylesoftware.htmlunit.WebClient; 
import com.gargoylesoftware.htmlunit.WebResponse; 
import com.gargoylesoftware.htmlunit.WebWindowEvent; 
import com.gargoylesoftware.htmlunit.WebWindowListener; 
import com.gargoylesoftware.htmlunit.util.NameValuePair; 

public class HtmlUnitDownloadFile { 

    protected String baseUrl; 
    protected static WebDriver driver; 

    @Before 
    public void openBrowser() { 
     baseUrl = "http://localhost/teste.html"; 
     driver = new CustomHtmlUnitDriver(); 
     ((HtmlUnitDriver) driver).setJavascriptEnabled(true); 

    } 


    @Test 
    public void downloadAFile() throws Exception { 

     driver.get(baseUrl); 
     driver.findElement(By.linkText("click to Downloadfile")).click(); 

    } 

    public class CustomHtmlUnitDriver extends HtmlUnitDriver { 

      // This is the magic. Keep a reference to the client instance 
      protected WebClient modifyWebClient(WebClient client) { 


      ConfirmHandler okHandler = new ConfirmHandler(){ 
        public boolean handleConfirm(Page page, String message) { 
         return true; 
        } 
      }; 
      client.setConfirmHandler(okHandler); 

      client.addWebWindowListener(new WebWindowListener() { 

       public void webWindowOpened(WebWindowEvent event) { 
        // TODO Auto-generated method stub 

       } 

       public void webWindowContentChanged(WebWindowEvent event) { 

        WebResponse response = event.getWebWindow().getEnclosedPage().getWebResponse(); 
        System.out.println(response.getLoadTime()); 
        System.out.println(response.getStatusCode()); 
        System.out.println(response.getContentType()); 

        List<NameValuePair> headers = response.getResponseHeaders(); 
        for(NameValuePair header: headers){ 
         System.out.println(header.getName() + " : " + header.getValue()); 
        } 

        // Change or add conditions for content-types that you would to like 
        // receive like a file. 
        if(response.getContentType().equals("text/plain")){ 
         getFileResponse(response, "target/testDownload.war"); 
        } 



       } 

       public void webWindowClosed(WebWindowEvent event) { 



       } 
      });   

      return client; 
      } 


    } 

    public static void getFileResponse(WebResponse response, String fileName){ 

     InputStream inputStream = null; 

     // write the inputStream to a FileOutputStream 
     OutputStream outputStream = null; 

     try {  

      inputStream = response.getContentAsStream(); 

      // write the inputStream to a FileOutputStream 
      outputStream = new FileOutputStream(new File(fileName)); 

      int read = 0; 
      byte[] bytes = new byte[1024]; 

      while ((read = inputStream.read(bytes)) != -1) { 
       outputStream.write(bytes, 0, read); 
      } 

      System.out.println("Done!"); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (inputStream != null) { 
       try { 
        inputStream.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      if (outputStream != null) { 
       try { 
        // outputStream.flush(); 
        outputStream.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

      } 
     } 

    } 

} 
+1

मुझे खेद है, लेकिन मुझे यह नहीं मिला है, मैं 'संशोधित' क्लाइंट में 'वेबक्लिएंट' के संदर्भ को कहां या कैसे रख रहा हूं ...... धन्यवाद –

+1

https://selenium.googlecode.com/svn/trunk/ डॉक्स/API/जावा/org/openqa/सेलेनियम/htmlunit/HtmlUnitDriver.html # modifyWebClient (com.gargoylesoftware.htmlunit.WebClient) Anudeep Samaiya सुपर क्लास की एक विधि .. है हम यह पुष्टि करने के लिए एक संभाल जोड़ने यह ओवरराइड कर सकते हैं डाउनलोड फ़ाइल की खिड़कियां .. लेकिन आपको अपने मामले में प्रतीक्षा की गई सामग्री प्रकार को संशोधित करने की आवश्यकता है। –

+0

वास्तव में यह एक जादू करता है .. आसानी से काम करता है। – viralpatel

2

वहाँ एक आसान तरीका है अगर आप सेलेनियम के साथ HtmlUnit लपेटकर में नहीं कर रहे हैं है। विस्तारित WebWindowListener के साथ बस HtmlUnit के WebClient प्रदान करें।

आप आसान स्ट्रीम प्रतिलिपि के लिए अपाचे commons.io का भी उपयोग कर सकते हैं।

WebClient webClient = new WebClient(); 
webClient.addWebWindowListener(new WebWindowListener() { 
    public void webWindowOpened(WebWindowEvent event) { } 

    public void webWindowContentChanged(WebWindowEvent event) { 
     // Change or add conditions for content-types that you would 
     // to like receive like a file. 
     if (response.getContentType().equals("text/plain")) { 
      try { 
       IOUtils.copy(response.getContentAsStream(), new FileOutputStream("downloaded_file")); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 

    public void webWindowClosed(WebWindowEvent event) {} 
}); 
1
final WebClient webClient = new WebClient(BrowserVersion.CHROME); 
     webClient.getOptions().setTimeout(2000); 
     webClient.getOptions().setThrowExceptionOnScriptError(false); 
     webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); 
     webClient.waitForBackgroundJavaScript(2000); 

     //get General page 
     final HtmlPage page = webClient.getPage("http://your"); 

     //get Frame 
     final HtmlPage frame = ((HtmlPage) 
     page.getFrameByName("Frame").getEnclosedPage()); 

     webClient.setConfirmHandler(new ConfirmHandler() { 
      public boolean handleConfirm(Page page, String message) { 
       return true; 
      } 
     }); 

     //get element file 
     final DomElement file = mainFrame.getElementByName("File"); 

     final InputStream xls = file.click().getWebResponse().getContentAsStream(); 

     assertNotNull(xls); 
    } 
-1

बाहर डाउनलोड URL चित्रा, और सूची में यह स्क्रैप। डाउनलोड यूआरएल से हम इस कोड का उपयोग कर पूरी फाइल प्राप्त कर सकते हैं।

try{ 
     String path = "your destination path"; 
     List<HtmlElement> downloadfiles = (List<HtmlElement>) page.getByXPath("the tag you want to scrape"); 
     if (downloadfiles.isEmpty()) { 
      System.out.println("No items found !"); 
     } else { 
      for (HtmlElement htmlItem : downloadfiles) { 
       String DownloadURL = htmlItem.getHrefAttribute(); 

       Page invoicePdf = client.getPage(DownloadURL); 
       if (invoicePdf.getWebResponse().getContentType().equals("application/pdf")) { 
        System.out.println("creatign PDF:"); 
        IOUtils.copy(invoicePdf.getWebResponse().getContentAsStream(), 
          new FileOutputStream(path + "file name")); 
       } 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
संबंधित मुद्दे