How to get all images url from website in Selenium Webdriver using java.


 import java.util.List;  
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.Keys;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class Getallimagefromwebsite {  
 static WebDriver driver;  
 static List<webelement> imageurls&lt;WebElement&gt;, links; //</webelement>  
 public static void main(String[] a) throws InterruptedException {  
 // Initialize Firefox driver   
 driver = new FirefoxDriver();  
 //Maximize browser window   
 driver.manage().window().maximize();  
 String websiteaddress = "http://www.google.com";  
 //Go to website   
 driver.get(websiteaddress);  
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
 //get all url of page  
 links = driver.findElements(By.tagName("a"));  
 for (int k = 0; k &lt; links.size(); k++) {  
 // check url not null, empty and only current website   
  if (!(links.get(k).getAttribute("href") == null) &amp;&amp; !(links.get(k).getAttribute("href").equals("")) &amp;&amp; (links.get(k).getAttribute("href").contains("google.com")) &amp;&amp; !(links.get(k).getAttribute("href").contains("@"))) {  
  // Open new tab  
 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");  
 driver.get(links.get(k).getAttribute("href"));  
 driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);  
  // get all images  
 imageurls = driver.findElements(By.tagName("img"));  
 System.err.println("Page URL =: " + driver.getCurrentUrl());  
 //get all images url  
 for (int m = 0; m &lt; imageurls.size(); m++) {  
 if (!(imageurls.get(m).getAttribute("src").equals("")) &amp;&amp; !(imageurls.get(m).getAttribute("src") == null)) {  
 System.out.println(imageurls.get(m).getAttribute("src"));  
           }  
         }  
 // close open tab  
 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");  
 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t");  
 driver.switchTo().defaultContent();  
 Thread.sleep(7000);  
       }  
     }  
 driver.quit();  
   }  
 }  

4 comments:

  1. "this was vey ideal blog for vehicles. if you need to see more , check this site Latest vehicles and bikes

    "

    ReplyDelete
  2. Thanks for sharing this content in your post its really good by the way if anyone looking for Core and Advanced Java training institute in delhi so contact here +91-9311002620 visit https://www.htsindia.com/java-training-courses

    ReplyDelete
  3. Thanks for sharing this content in your post its really good by the way if anyone looking for Core and Advanced Java training institute in delhi so contact here +91-9311002620 visit https://www.htsindia.com/java-training-courses

    ReplyDelete
  4. Thank you for sharing this post If anyone looking for best Software training institute in Delhi Contact Here-+91-9311002620 Or Visit our website https://www.htsindia.com/software-testing-training-courses

    ReplyDelete