2

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

Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
asked May 20, 2016 at 6:39
2
  • check the link by using http request and responce so that we come to know that link is active or not Commented May 20, 2016 at 7:09
  • Well, if you don't want to assert the PDF content or the URL, you can take a screenshot and somehow compare it with an existing screenshot or image of the PDF opened in a browser! Commented May 20, 2016 at 7:11

1 Answer 1

1

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;
 }
}
answered May 20, 2016 at 17:06
4
  • Can you please explain me what the above does i am not aware of how this will work,Please explain me so that i will get to know if this will help me in solving my issue Commented May 25, 2016 at 7:20
  • i have tried the above code in my script but even the content is loading or not loading the i am getting the response code as 403 Commented May 25, 2016 at 9:59
  • How do i use the response code to know whether the url is loading or not i din't get that point please explain Commented May 25, 2016 at 10:32
  • Above code tries to open a connection with given URL and tries to check its response. For any successful connection website returns response code 200(HTTP_OK). If website refuses connection then it would return 403 or 404. You must have seen "404-Page Not found" error for some pages. I've just checked this code locally for pdf and it returning successful response. Could you please pass URL manually and see its response? Would you able to share links here so that I can have a look? Commented May 25, 2016 at 13:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.