How to validate if PDF is loaded or not in Selenium Webdriver?



  • I have a link in Website which will redirect to PDF document after clicking I would like to check if PDF is loaded in the browser or not using selenium

    I do not want to compare the text/content in the loaded PDF or checking the pdf extension in the URL.

    Please let me know how to automate the above scenario appreciate the response ASAP



  • why don't you check normal http response. That will also work for a normal http page. Here is the code sample that you can use.

    @Test
    
        public void sampleTest(){ //test method
            //your code
            boolean abletoOpenPDF=linkResponse(driver.getCurrentUrl());
    
        }
    
        public static boolean linkResponse(String url){
            try {
                HttpURLConnection.setFollowRedirects(false);
                HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
                con.setRequestMethod("HEAD");
                return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
            }
            catch (Exception e) {
                e.printStackTrace();
                return false;
            }
    
    
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2