{"id":2693,"date":"2020-04-22T17:19:46","date_gmt":"2020-04-22T11:49:46","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2693"},"modified":"2025-06-11T07:49:54","modified_gmt":"2025-06-11T11:49:54","slug":"selenium-webdriver-handling-images","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/selenium-webdriver-handling-images\/","title":{"rendered":"Selenium Webdriver \u2013 Handling Images"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><b>Accessing Image Links<\/b><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">In this tutorial, we learn about Handling images using Selenium Webdriver. Image links in web pages are the links that represent an image. When we click an image it navigates to a different window or a page.\u00a0Since Image links have no link texts we cannot use the By.linkText() and By.partialLinkText() to access the image links. In this case, we use either By.cssSelector or By.Xpath.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Consider the below example from a <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium certification course<\/a>, to access the \u201cFacebook\u201d logo on the upper left portion of Facebook\u2019s Password Recovery page.<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" width=\"761\" height=\"274\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_10.png\" alt=\"Selenium Webdriver \u2013 Handling Images\" class=\"wp-image-2694\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_10.png 761w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_10-300x108.png 300w\" sizes=\"(max-width: 761px) 100vw, 761px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, we will create a test case step by step to understand how to access the image.<\/span><\/p>\n\n\n\n<p><b>Step 1:<\/b><span style=\"font-weight: 400;\"> Launch the Eclipse IDE<\/span><\/p>\n\n\n\n<p><b>Step 2: <\/b><span style=\"font-weight: 400;\">Right-click on the src folder and click on the New &gt; class.<\/span><\/p>\n\n\n\n<p><b>Step 3: <\/b><span style=\"font-weight: 400;\">Enter the Class Name as (Image)<\/span><\/p>\n\n\n\n<p><b>Step 4:<\/b><span style=\"font-weight: 400;\"> Invoke the Google Chrome browser and set the system property to the path of your chromedriver.exe file.<\/span><\/p>\n\n\n\n<p><b>Step 5:<\/b><span style=\"font-weight: 400;\"> Navigate to the \u201cFacebook\u201d website.<\/span><\/p>\n\n\n\n<p><strong>Here is the complete code for above scenario:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><b>import<\/b><span style=\"font-weight: 400;\">&nbsp;org.openqa.selenium.WebDriver;&nbsp;&nbsp;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\">&nbsp;org.openqa.selenium.chrome.ChromeDriver;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;<\/span>\n<b>public<\/b><span style=\"font-weight: 400;\">&nbsp;<\/span><b>class<\/b><span style=\"font-weight: 400;\">&nbsp;Image {&nbsp;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;<\/span><b>public<\/b><span style=\"font-weight: 400;\">&nbsp;<\/span><b>static<\/b><span style=\"font-weight: 400;\">&nbsp;<\/span><b>void<\/b><span style=\"font-weight: 400;\">&nbsp;main(String&#91;]&nbsp;args)&nbsp;{&nbsp;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">String baseUrl = <\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">https:\/\/www.facebook.com\/login\/identify\/?ctx=recover<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">\/\/&nbsp;TODO&nbsp;Auto-generated&nbsp;method&nbsp;stub<\/span><span style=\"font-weight: 400;\">&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;System.setProperty(<\/span><span style=\"font-weight: 400;\">\"webdriver.chrome.driver\"<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\u201c<\/span><span style=\"font-weight: 400;\"> D:\\\\Drivers\\\\geckodriver.exe<\/span><span style=\"font-weight: 400;\"> \"<\/span><span style=\"font-weight: 400;\">);&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;WebDriver&nbsp;driver&nbsp;=&nbsp;<\/span><span style=\"font-weight: 400;\">new<\/span><span style=\"font-weight: 400;\">&nbsp;ChromeDriver();&nbsp;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">driver.get(baseUrl);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}&nbsp;&nbsp;<\/span><\/code><\/pre>\n\n\n\n<p><b>Step 6:<\/b><span style=\"font-weight: 400;\"> Now we try to click on the &#8220;Facebook&#8221; logo on the upper left portion by inspecting its HTML code.<\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"764\" height=\"280\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_11.png\" alt=\"\" class=\"wp-image-2696\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_11.png 764w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_11-300x110.png 300w\" sizes=\"(max-width: 764px) 100vw, 764px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><b>Note the title of a \u201cFacebook\u201d image.<\/b><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"721\" height=\"71\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_12.png\" alt=\"\" class=\"wp-image-2697\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_12.png 721w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_12-300x30.png 300w\" sizes=\"(max-width: 721px) 100vw, 721px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><b>Step 7:<\/b><span style=\"font-weight: 400;\"> Write the code to handle the \u201cFacebook\u201d image<\/span><b>.<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">import org.openqa.selenium.By;<\/span>\n<span style=\"font-weight: 400;\">import org.openqa.selenium.WebDriver;<\/span>\n<span style=\"font-weight: 400;\">import org.openqa.selenium.chrome.ChromeDriver;<\/span>\n\n<span style=\"font-weight: 400;\">public class Image {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;<\/span><span style=\"font-weight: 400;\">&nbsp;public static void main(String&#91;] args) {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String baseUrl = <\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">https:\/\/www.facebook.com\/login\/identify\/?ctx=recover<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">;&nbsp;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.setProperty(\"webdriver.chrome.driver\",\"<\/span><span style=\"font-weight: 400;\"> D:\\\\Drivers\\\\geckodriver.exe<\/span> <span style=\"font-weight: 400;\">\");<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebDriver driver = new ChromeDriver();<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.get(baseUrl);<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/click on the \"Facebook\" logo<\/span>\n\n<span style=\"font-weight: 400;\">driver.findElement(By.cssSelector(\"a&#91;title=\\\"Go to Facebook home\\\"]\")).click();<\/span>\n\n<span style=\"font-weight: 400;\">\/\/verify that we are back on Facebook's homepage<\/span>\n\n<span style=\"font-weight: 400;\">if (driver.getTitle().equals(\"Facebook - log in or sign up\"))&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">{<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"We are back to Facebook's homepage\");<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"We are NOT at Facebook's homepage\");<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/span>\n\n<span style=\"font-weight: 400;\">driver.close();<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/span>\n\n<span style=\"font-weight: 400;\">}<\/span><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Finding All Broken links using Selenium Webdriver<\/b><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><b>What are Broken Links?<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">The URLs or links which are not reachable are called <a href=\"https:\/\/www.h2kinfosys.com\/blog\/understanding-broken-links-in-selenium-webdriver\/\">broken links<\/a>. The URLs or links may be down due to some server problem. Every URL will always have a status code 200 which is valid. For every different purpose, there are different HTTP status codes. 400 and 500 are the HTTP status codes for invalid requests. 400 status code mainly occur due to client-side error, and 500 status code mainly occur due to server response error. We will mostly unable to confirm whether the link is working or not until and unless we click and confirm it.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Moving a Mouse On A Object In Selenium | Selenium Webdriver Online Training | Selenium Training H2K\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/INGM1RgWZYA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><b>Reason to check the broken links<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">You should always make confirm that there are no broken links present on the website because the end-user should not land into an error page whenever they click on any link.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Manual checking for links is a boring task, and it takes a full day to verify every link present on the webpage and you will not get the same efficiency and interest as well every time. It is a time taking process to manually check for the links as each webpage may have a huge number of links and we have to repeat the manual process for all pages. To overcome this problem the automation process is a more apt solution.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><b>Checking for Broken Links and Images<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Below are the steps to check for all the broken links<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Based on anchor tag &lt;a&gt; collect all the links in a web page<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Send HTTP request for every link and read the HTTP response code<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Based on displayed HTTP response code find out whether the link is broken or valid link<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Repeat the same process for all the other links captured.<\/span><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><b>Code for checking the broken links on a webpage<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Below is the whole <a href=\"https:\/\/www.h2kinfosys.com\/blog\/wait-in-selenium-webdriver\/\">Selenium WebDriver<\/a> code to find the broken images.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Let&#8217;s create a test case in which we will automate the following scenarios to handle Broken Links:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Invoke a Google chrome browser.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Open URL: &#8220;https:\/\/makemysushi.com\/404?&#8221;<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Find all the broken images in the website.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">Now, we will create a test case step by step to understand how to handle broken images in WebDriver.<\/span><\/p>\n\n\n\n<p><b>Step 1:<\/b> <span style=\"font-weight: 400;\">Create a New <a href=\"https:\/\/www.h2kinfosys.com\/blog\/design-a-class-in-java\/\">Java class<\/a><\/span><\/p>\n\n\n\n<p><b>Name:<\/b> <span style=\"font-weight: 400;\">BrokenLinkTest<\/span><\/p>\n\n\n\n<p><b>Step 2:<\/b> <span style=\"font-weight: 400;\">Invoke the Google Chrome browser and set the system property to the path of your chromedriver.exe file.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Here is the sample code to set Chrome driver system property:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">&nbsp;\/\/ System Property for Chrome Driver\nSystem.setProperty(<\/span><span style=\"font-weight: 400;\">\"webdriver.chrome.driver\"<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\u201c D:\\\\Drivers\\\\geckodriver.exe \"<\/span><span style=\"font-weight: 400;\">);&nbsp;&nbsp;<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">After that, we have to initialize the Chrome driver using <a href=\"https:\/\/github.com\/SeleniumHQ\/selenium\/wiki\/ChromeDriver\" rel=\"nofollow noopener\" target=\"_blank\">ChromeDriver <\/a>Class.<\/span> <span style=\"font-weight: 400;\">Below is the sample code to initialize Chrome driver using ChromeDriver class.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">\/\/ Instantiate a ChromeDriver class.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">WebDriver driver=new ChromeDriver();&nbsp;&nbsp;<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">We will get the below code to launch the Google Chrome browser after combining both of the above codes.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">System.setProperty(<\/span><span style=\"font-weight: 400;\">\"webdriver.chrome.driver\"<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\u201c D:\\\\Drivers\\\\geckodriver.exe \"<\/span><span style=\"font-weight: 400;\">);&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">WebDriver driver=new ChromeDriver();&nbsp; <\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">After that, we need to navigate to the desired URL.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Below is the sample code to navigate to the desired URL:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">\/\/ Launch Website&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">driver.navigate().to(\"https:\/\/makemysushi.com\/404?\");&nbsp;&nbsp;<\/span><\/pre>\n\n\n\n<p><strong>Here is the complete code for the above scenario:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">import org.openqa.selenium.WebDriver;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">import org.openqa.selenium.chrome.ChromeDriver;&nbsp;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">public class BrkoenLinkTest {&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ System Property for Chrome Driver&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;System.setProperty(<\/span><span style=\"font-weight: 400;\">\"webdriver.chrome.driver\"<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">\u201c D:\\\\Drivers\\\\geckodriver.exe \"<\/span><span style=\"font-weight: 400;\">);&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Instantiate a ChromeDriver class.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;WebDriver driver=new ChromeDriver();&nbsp;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;<\/span><span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Launch Website&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">driver.get(\"https:\/\/makemysushi.com\/404?\");&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">}&nbsp; <\/span><\/code><\/pre>\n\n\n\n<p><b>Step 3:<\/b><span style=\"font-weight: 400;\"> Now we will try to find all the broken links in a website.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Follow the below steps to locate the drop-down menu on the web page.<br><\/span><span style=\"font-weight: 400;\">Open URL: <\/span><a href=\"https:\/\/makemysushi.com\" rel=\"nofollow noopener\" target=\"_blank\"><span style=\"font-weight: 400;\">https:\/\/makemysushi.com<\/span><\/a><\/p>\n\n\n\n<p><b>Complete Code to find all the links<\/b><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>import<\/b><span style=\"font-weight: 400;\"> java.io.IOException;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> java.net.HttpURLConnection;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> java.net.MalformedURLException;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> java.net.URL;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> java.util.ArrayList;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> java.util.List;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> java.util.concurrent.TimeUnit;<\/span>\n\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.By;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.WebDriver;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.WebElement;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.chrome.ChromeDriver;<\/span>\n\n<b>public<\/b> <b>class<\/b><span style=\"font-weight: 400;\"> BrokenLinkTest {<\/span>\n\n<b>public<\/b> <b>static<\/b> <b>void<\/b><span style=\"font-weight: 400;\"> main(String[] <\/span><span style=\"font-weight: 400;\">args<\/span><span style=\"font-weight: 400;\">) <\/span><b>throws<\/b><span style=\"font-weight: 400;\"> MalformedURLException, IOException {<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ <\/span><b>TODO<\/b><span style=\"font-weight: 400;\"> Auto-generated method stub<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; System.<\/span><i><span style=\"font-weight: 400;\">setProperty<\/span><\/i><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"webdriver.chrome.driver\"<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\">\" D:\\\\Drivers\\\\geckodriver.exe \"<\/span><span style=\"font-weight: 400;\">);<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; WebDriver <\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\"> = <\/span><b>new<\/b><span style=\"font-weight: 400;\"> ChromeDriver();<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.manage().window().maximize(); <\/span><span style=\"font-weight: 400;\">\/\/maximize window<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.manage().deleteAllCookies(); <\/span><span style=\"font-weight: 400;\">\/\/delete all the cookies<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">\/\/dynamic wait<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.manage().timeouts().pageLoadTimeout(40, TimeUnit.<\/span><b><i>SECONDS<\/i><\/b><span style=\"font-weight: 400;\">);<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.manage().timeouts().implicitlyWait(10, TimeUnit.<\/span><b><i>SECONDS<\/i><\/b><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">\/\/enter <\/span><span style=\"font-weight: 400;\">Url<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">\"https:\/\/makemysushi.com\/404?\"<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">\/\/get the list of all the links and images<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; List&lt;WebElement&gt; <\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\"> =&nbsp; <\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.findElements(By.<\/span><i><span style=\"font-weight: 400;\">tagName<\/span><\/i><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"a\"<\/span><span style=\"font-weight: 400;\">));<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.addAll(<\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.findElements(By.<\/span><i><span style=\"font-weight: 400;\">tagName<\/span><\/i><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"img\"<\/span><span style=\"font-weight: 400;\">)));<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">\"size of full links and images--&gt;\"<\/span><span style=\"font-weight: 400;\">+ <\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.size());<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; List&lt;WebElement&gt; <\/span><span style=\"font-weight: 400;\">activeLinks<\/span><span style=\"font-weight: 400;\"> = <\/span><b>new<\/b><span style=\"font-weight: 400;\"> ArrayList&lt;WebElement&gt;();<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">\/\/iterate linksList : exclude all the links or images which don\u2019t have an <\/span><span style=\"font-weight: 400;\">href<\/span><span style=\"font-weight: 400;\"> attribute<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; <\/span><b>for<\/b><span style=\"font-weight: 400;\">(<\/span><b>int<\/b> <span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">=0; <\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">&lt;<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.size(); <\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">++) {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">).getAttribute(<\/span><span style=\"font-weight: 400;\">\"href\"<\/span><span style=\"font-weight: 400;\">));\n\n<\/span><b>if<\/b><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">).getAttribute(<\/span><span style=\"font-weight: 400;\">\"href\"<\/span><span style=\"font-weight: 400;\">) !=<\/span><b>null<\/b><span style=\"font-weight: 400;\"> &amp;&amp; \n               (!<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">).getAttribute(<\/span><span style=\"font-weight: 400;\">\"href\"<\/span><span style=\"font-weight: 400;\">).contains(<\/span><span style=\"font-weight: 400;\">\"javascript\"<\/span><span style=\"font-weight: 400;\">))) {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; <\/span><span style=\"font-weight: 400;\">activeLinks<\/span><span style=\"font-weight: 400;\">.add(<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">));<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; }<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; }<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"font-weight: 400;\">\/\/get the size of active links list:<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">\"size of active links and images--&gt;\"<\/span><span style=\"font-weight: 400;\">+ <\/span><span style=\"font-weight: 400;\">activeLinks<\/span><span style=\"font-weight: 400;\">.size());<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">\/\/check the <\/span><span style=\"font-weight: 400;\">href<\/span> <span style=\"font-weight: 400;\">url<\/span><span style=\"font-weight: 400;\"> with <\/span><span style=\"font-weight: 400;\">httpconnection<\/span> <span style=\"font-weight: 400;\">api<\/span><span style=\"font-weight: 400;\">:<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; <\/span><b>for<\/b><span style=\"font-weight: 400;\">(<\/span><b>int<\/b> <span style=\"font-weight: 400;\">j<\/span><span style=\"font-weight: 400;\">=0; <\/span><span style=\"font-weight: 400;\">j<\/span><span style=\"font-weight: 400;\">&lt;<\/span><span style=\"font-weight: 400;\">activeLinks<\/span><span style=\"font-weight: 400;\">.size(); <\/span><span style=\"font-weight: 400;\">j<\/span><span style=\"font-weight: 400;\">++) {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; HttpURLConnection <\/span><span style=\"font-weight: 400;\">connection<\/span><span style=\"font-weight: 400;\"> = (HttpURLConnection) <\/span><b>new<\/b><span style=\"font-weight: 400;\"> \n                       URL(<\/span><span style=\"font-weight: 400;\">activeLinks<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">j<\/span><span style=\"font-weight: 400;\">).getAttribute(<\/span><span style=\"font-weight: 400;\">\"href\"<\/span><span style=\"font-weight: 400;\">)).openConnection();<\/span>\n<span style=\"font-weight: 400;\"> &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; <\/span><span style=\"font-weight: 400;\">connection<\/span><span style=\"font-weight: 400;\">.connect();<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; String <\/span><span style=\"font-weight: 400;\">response<\/span><span style=\"font-weight: 400;\"> =<\/span><span style=\"font-weight: 400;\">connection<\/span><span style=\"font-weight: 400;\">.getResponseMessage();<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; <\/span><span style=\"font-weight: 400;\">connection<\/span><span style=\"font-weight: 400;\">.disconnect();<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">activeLinks<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">j<\/span><span style=\"font-weight: 400;\">).getAttribute(<\/span><span style=\"font-weight: 400;\">\"href\"<\/span><span style=\"font-weight: 400;\">) +<\/span><span style=\"font-weight: 400;\">\"----&gt;\"<\/span><span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\">response<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; }\n   }\n\n}<\/span><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Code Explanation<\/b><\/h2>\n\n\n\n<p><b>Step 1: Import Packages<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">In addition to default <a href=\"https:\/\/www.h2kinfosys.com\/blog\/java-language-packages\/\">packages <\/a>import below package<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><b>import<\/b><span style=\"font-weight: 400;\"> java.net.HttpURLConnection;<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Using the above methods in the above package we can send HTTP requests and get the HTTP response code.<\/span><\/p>\n\n\n\n<p><b>Step 2: Get all the links and images in a web page<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Store all the identified links and images in a list&nbsp;<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">&nbsp;List&lt;WebElement&gt; <\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\"> =&nbsp; <\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.findElements(By.<\/span><i><span style=\"font-weight: 400;\">tagName<\/span><\/i><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"a\"<\/span><span style=\"font-weight: 400;\">));<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; <\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.addAll(<\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.findElements(By.<\/span><i><span style=\"font-weight: 400;\">tagName<\/span><\/i><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"img\"<\/span><span style=\"font-weight: 400;\">)));<\/span><\/pre>\n\n\n\n<p><b>Step 3:<\/b><span style=\"font-weight: 400;\"> Identifying and validating the URL<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Get href of an anchor tag<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">).getAttribute(<\/span><span style=\"font-weight: 400;\">\"href\"<\/span><span style=\"font-weight: 400;\">));<\/span><\/pre>\n\n\n\n<p><b>Step 4:<\/b><span style=\"font-weight: 400;\"> Iterate linksList: exclude all the links or images which don\u2019t have an <\/span><span style=\"font-weight: 400;\">href<\/span><span style=\"font-weight: 400;\"> attribute<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><b>for<\/b><span style=\"font-weight: 400;\">(<\/span><b>int<\/b> <span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">=0; <\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">&lt;<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.size(); <\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">++) {<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">).getAttribute(<\/span><span style=\"font-weight: 400;\">\"href\"<\/span><span style=\"font-weight: 400;\">));<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; <\/span><b>if<\/b><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">).getAttribute(<\/span><span style=\"font-weight: 400;\">\"href\"<\/span><span style=\"font-weight: 400;\">) !=<\/span><b>null<\/b><span style=\"font-weight: 400;\"> &amp;&amp; \n                  (!<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">).getAttribute(<\/span><span style=\"font-weight: 400;\">\"href\"<\/span><span style=\"font-weight: 400;\">).contains(<\/span><span style=\"font-weight: 400;\">\"javascript\"<\/span><span style=\"font-weight: 400;\">))) {<\/span>\n<span style=\"font-weight: 400;\">&nbsp; &nbsp; <\/span> <span style=\"font-weight: 400;\">&nbsp; <\/span><span style=\"font-weight: 400;\">activeLinks<\/span><span style=\"font-weight: 400;\">.add(<\/span><span style=\"font-weight: 400;\">linksList<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">));<\/span><\/code><\/pre>\n\n\n\n<p><b>Step 5:<\/b> <b>Get the size of active links lists<\/b><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">&nbsp;System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">\"size of active links and images--&gt;\"<\/span><span style=\"font-weight: 400;\">+ <\/span><span style=\"font-weight: 400;\">activeLinks<\/span><span style=\"font-weight: 400;\">.size());<\/span><\/pre>\n\n\n\n<p><b>Step 6:<\/b> <b>Send Http request<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">HttpURLConnection class has methods to send HTTP request and get the HTTP response code. So, output of openConnection() method (URLConnection) is type casted to HttpURLConnection.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">connection = (HttpURLConnection)(new URL(url).openConnection());<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">On invoking connect() method, the actual connection is established and the request is sent to the URL.<br><\/span><span style=\"font-weight: 400;\">&nbsp; &nbsp;connection.connect();<\/span><\/p>\n\n\n\n<p><b>Step 7:<\/b> <b>Validating Links<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">We can get response code for the request by using the method getResponseCode()&nbsp;<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">response =connection.getResponseMessage();<\/span><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Based on response code we got we will try to check the link status.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Thus, we can get all the links from the web page and print whether the links are valid links or broken links.<\/span><\/p>\n\n\n\n<p><b>Code for checking the broken images on a webpage<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><b>import<\/b><span style=\"font-weight: 400;\"> java.io.IOException;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> java.net.HttpURLConnection;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> java.net.URL;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> java.util.List;<\/span>\n\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.By;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.WebDriver;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.WebElement;<\/span>\n<b>import<\/b><span style=\"font-weight: 400;\"> org.openqa.selenium.chrome.ChromeDriver;<\/span>\n\n<b>public<\/b> <b>class<\/b><span style=\"font-weight: 400;\"> FindBrokenImages {<\/span>\n\n<b>public<\/b> <b>static<\/b> <b>void<\/b><span style=\"font-weight: 400;\"> main(String&#91;] <\/span><span style=\"font-weight: 400;\">args<\/span><span style=\"font-weight: 400;\">) <\/span><b>throws<\/b><span style=\"font-weight: 400;\"> IOException {<\/span>\n<span style=\"font-weight: 400;\">\/\/ <\/span><b>TODO<\/b><span style=\"font-weight: 400;\"> Auto-generated method stub<\/span>\n\n<span style=\"font-weight: 400;\">System.<\/span><i><span style=\"font-weight: 400;\">setProperty<\/span><\/i><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"webdriver.chrome.driver\"<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\">\" D:\\\\Drivers\\\\geckodriver.exe \"<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebDriver <\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\"> = <\/span><b>new<\/b><span style=\"font-weight: 400;\"> ChromeDriver();<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.manage().window().maximize(); <\/span><span style=\"font-weight: 400;\">\/\/maximize window<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">\"http:\/\/google.com\"<\/span><span style=\"font-weight: 400;\">);<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;WebElement&gt; <\/span><span style=\"font-weight: 400;\">links<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">driver<\/span><span style=\"font-weight: 400;\">.findElements(By.<\/span><i><span style=\"font-weight: 400;\">tagName<\/span><\/i><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">\"img\"<\/span><span style=\"font-weight: 400;\">));<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><b>int<\/b> <span style=\"font-weight: 400;\">invalidImageCount<\/span><span style=\"font-weight: 400;\"> = 0;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><b>for<\/b><span style=\"font-weight: 400;\"> (<\/span><b>int<\/b> <span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\"> = 0; <\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\"> &lt; <\/span><span style=\"font-weight: 400;\">links<\/span><span style=\"font-weight: 400;\">.size(); <\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">++) {<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">links<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">));<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String <\/span><span style=\"font-weight: 400;\">linkURL<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">links<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">).getAttribute(<\/span><span style=\"font-weight: 400;\">\"src\"<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">links<\/span><span style=\"font-weight: 400;\">.get(<\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">).getText());<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URL <\/span><span style=\"font-weight: 400;\">url<\/span><span style=\"font-weight: 400;\"> = <\/span><b>new<\/b><span style=\"font-weight: 400;\"> URL(<\/span><span style=\"font-weight: 400;\">linkURL<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpURLConnection <\/span><span style=\"font-weight: 400;\">http<\/span><span style=\"font-weight: 400;\"> = (HttpURLConnection) <\/span><span style=\"font-weight: 400;\">url<\/span><span style=\"font-weight: 400;\">.openConnection();<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"font-weight: 400;\">http<\/span><span style=\"font-weight: 400;\">.setRequestProperty(<\/span><span style=\"font-weight: 400;\">\"User-Agent\"<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\">\"Chrome\/23.0.1271.95\"<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"font-weight: 400;\">http<\/span><span style=\"font-weight: 400;\">.setConnectTimeout(10000);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"font-weight: 400;\">http<\/span><span style=\"font-weight: 400;\">.setReadTimeout(20000);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><b>int<\/b> <span style=\"font-weight: 400;\">statusCode<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">http<\/span><span style=\"font-weight: 400;\">.getResponseCode();<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><b>if<\/b><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">statusCode<\/span><span style=\"font-weight: 400;\">==404||<\/span><span style=\"font-weight: 400;\">statusCode<\/span><span style=\"font-weight: 400;\">==500){<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span> <span style=\"font-weight: 400;\">invalidImageCount<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">invalidImageCount<\/span><span style=\"font-weight: 400;\">+1;<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">linkURL<\/span><span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\">\"and its Status codes is:\"<\/span><span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\">statusCode<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.<\/span><b><i>out<\/i><\/b><span style=\"font-weight: 400;\">.println(<\/span><span style=\"font-weight: 400;\">\"total number of broken images are: \"<\/span><span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\">invalidImageCount<\/span><span style=\"font-weight: 400;\">);<\/span>\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span>\n<span style=\"font-weight: 400;\">  }\n}\n<\/span><\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Accessing Image Links In this tutorial, we learn about Handling images using Selenium Webdriver. Image links in web pages are the links that represent an image. When we click an image it navigates to a different window or a page.\u00a0Since Image links have no link texts we cannot use the By.linkText() and By.partialLinkText() to access [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":2702,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[1801,555,554,553,1800,1802,448],"class_list":["post-2693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-broken-links","tag-checking-broken-images","tag-finding-broken-links","tag-handling-images","tag-http-response-code","tag-http-status-codes","tag-selenium-webdriver"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2693"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2693\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2702"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}