How to download embedded PDF from webpage using selenium?

后端 未结 2 1107
慢半拍i
慢半拍i 2020-12-12 03:30

I want to download embedded PDF from a webpage using selenium just like in this image. Embedded PDF image

For example, page like this: https://www.sebi.gov.in/enforc

相关标签:
2条回答
  • 2020-12-12 03:45

    Here is another way to grab the file without clicking/downloading. This method also helps you to download the file to your local machine if your tests are executed in Selenium Grid (remote nodes).

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import org.openqa.selenium.Cookie;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    
    public class FileDownloader extends MyPage(){
            public void downloadFile(){
    
              //grab the file download url from your download icon/button/element
             String src = iframe.getAttribute("src");
    
             driver.get(src); //driver object from 'MyPage.java'
    
              // Grab cookies from current driver session (authenticated cookie information 
              // is vital to download the file from 'src'
             StringBuilder cookies = new StringBuilder();
             for (Cookie cookie : driver.manage().getCookies()){
              String value = cookie.getName() + "=" + cookie.getValue();
              if (cookies.length() == 0 )
                cookies.append(value);
              else
                cookies.append(";").append(value);
             }
    
    
             try{
               HttpURLConnection con = (HttpURLConnection) new URL(src).openConnection();
               con.setRequestMethod("GET");
               con.addRequestProperty("Cookie",cookies.toString());
    
               //set your own download path, probably a dynamic file name with timestamp
               String downloadPath = System.getProperty("user.dir") + File.separator + "file.pdf";
               OutputStream outputStream = new FileOutputStream(new File(downloadPath));
               InputStream inputStream = con.getInputStream();
    
               int BUFFER_SIZE = 4096;
    
               byte[] buffer = new byte[BUFFER_SIZE];
               int bytesRead = -1;
    
               while((bytesRead = inputStream.read(buffer)) != -1)
                  outputStream.write(buffer, 0, bytesRead);
    
               outputStream.close();
              }catch(Exception e){
                // file download failed.
              }
    
            }
    }
    

    Here is how my dom looks like

    <iframe src="/files/downloads/pdfgenerator.aspx" id="frame01">
      #document
      <html>
        <body>
          <embed width="100%" height ="100%" src="about:blank" type="application/pdf" internalid="1234567890">
        </body>
      </html>
    </iframe>
    
    0 讨论(0)
  • 2020-12-12 04:04

    Here You go, description in code:

    =^..^=

    from selenium import webdriver
    import os
    
    # initialise browser
    browser = webdriver.Chrome(os.getcwd()+'/chromedriver')
    # load page with iframe
    browser.get('https://www.sebi.gov.in/enforcement/orders/jun-2019/adjudication-order-in-respect-of-three-entities-in-the-matter-of-prism-medico-and-pharmacy-ltd-_43323.html')
    
    # find pdf url
    pdf_url = browser.find_element_by_tag_name('iframe').get_attribute("src")
    # load page with pdf
    browser.get(pdf_url)
    # download file
    download = browser.find_element_by_xpath('//*[@id="download"]')
    download.click()
    
    0 讨论(0)
提交回复
热议问题