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<WebElement>, 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 < links.size(); k++) {
// check url not null, empty and only current website
if (!(links.get(k).getAttribute("href") == null) && !(links.get(k).getAttribute("href").equals("")) && (links.get(k).getAttribute("href").contains("google.com")) && !(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 < imageurls.size(); m++) {
if (!(imageurls.get(m).getAttribute("src").equals("")) && !(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();
}
}
How to get all images url from website in Selenium Webdriver using java.
Subscribe to:
Post Comments (Atom)
"this was vey ideal blog for vehicles. if you need to see more , check this site Latest vehicles and bikes
ReplyDelete"
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
ReplyDeleteThanks 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
ReplyDeleteThank 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