import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
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 Getallwebsiteimagedown {
static WebDriver driver;
static List<webelement> imageurls, links;
public static void main(String[] a) throws InterruptedException, MalformedURLException, IOException {
// Initialize Firefox driver
driver = new FirefoxDriver();
//Maximize browser window
driver.manage().window().maximize();
String websiteaddress = "https://www.flickr.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("flickr.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)) {
String[] imagename = imageurls.get(m).getAttribute("src").trim().split("\\/");
System.out.println(imageurls.get(m).getAttribute("src") + " " + imagename[imagename.length - 1]);
//download image
URL url = new URL(imageurls.get(m).getAttribute("src").trim());
InputStream in = new BufferedInputStream(url.openStream());
OutputStream out = new BufferedOutputStream(new FileOutputStream("image/" + imagename[imagename.length - 1]));
for (int i; (i = in.read()) != -1;) {
out.write(i);
}
in.close();
out.close();
}
}
// 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 download all images from website in Selenium Webdriver using java.
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<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 css value in selenium webdriver using java
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GetCssfontsizecolor {
public static void main(String[] a) {
// Initialize Firefox driver
WebDriver driver = new FirefoxDriver();
//Maximize browser window
driver.manage().window().maximize();
//Go to URL
driver.get("https://www.google.com/");
//Set selenium webdriver get timeout
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String font_name, font_size, font_color;
// Get font family name
font_name = driver.findElement(By.partialLinkText("Gmail")).getCssValue("font-family").trim();
// Get font size
font_size = driver.findElement(By.partialLinkText("Gmail")).getCssValue("font-size").trim();
// Get font color
font_color = driver.findElement(By.partialLinkText("Gmail")).getCssValue("color").trim();
System.out.println("font-family : " + font_name);
System.out.println("font-size : " + font_size);
System.out.println("font-color : " + font_color);
// Browser close
driver.close();
}
}
How to handle SSL certificate error in Selenium WebDriver using Java
FireFox Untrusted Connection
SSL certificate error handling for Google Chrome
SSL certificate error handling for IE
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class Firefoxuntrustconnection {
public static void main(String[] args) throws InterruptedException {
// Create Firefox Profile
FirefoxProfile profile = new FirefoxProfile();
// Accept Untrusted Certificates
profile.setAcceptUntrustedCertificates(true);
//Intialize Forfox driver
WebDriver driver = new FirefoxDriver(profile);
//Maximize browser window
driver.manage().window().maximize();
// Go to desired Untrusted website
driver.get("http://www.google.com");
Thread.sleep(5000);
//close Firefox browser
driver.quit();
}
}
SSL certificate error handling for Google Chrome
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class ChromeSSLcertificateerrorhandling {
public static void main(String[] args) throws InterruptedException {
DesiredCapabilities capability = DesiredCapabilities.chrome();
// Accept SSL certificate
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
//set the system property for Chrome
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
//create Google Chrome instance
WebDriver driver = new ChromeDriver(capability);
//Maximize browser window
driver.manage().window().maximize();
// Go to desired Untrusted website
driver.get("http://www.google.com");
Thread.sleep(5000);
//close Chrome browser
driver.quit();
}
}
SSL certificate error handling for IE
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class IESSLcertificateerrorhandling {
public static void main(String[] args) throws InterruptedException {
//set the system property for IE
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
//create IE instance
WebDriver driver = new InternetExplorerDriver();
// Go to desired Untrusted website
driver.get("http://www.google.com");
//Maximize browser window
driver.manage().window().maximize();
driver.navigate().to("javascript:document.getElementById('overridelink').click()");
Thread.sleep(5000);
//close IE browser
driver.quit();
}
}
Subscribe to:
Posts (Atom)