Handling Image

There are basically three types of Image present in web page i.e. Static Images, Image link and Image button.

Manual Action on Images

  • Return static image title
  • Check enabled status
  • Check display status
  • Click
package HandlingElementsInSelenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class HandlingImage {

	public static void main(String[] args) {
	System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
	WebDriver driver = new ChromeDriver();
	driver.manage().window().maximize();
	driver.get("https://www.google.com/");
	
	//Return static image title
	String imgName = driver.findElement(By.id("hplogo")).getAttribute("alt");
	System.out.println("Image name is :- "+imgName);
	
	//Click on image button
	driver.navigate().to("http://newtours.demoaut.com/");
	WebElement imgButton = driver.findElement(By.name("login"));
	
	String imgButtonName = imgButton.getAttribute("alt");
	System.out.println("Image Button name is :- "+imgButtonName);
	
	boolean status = imgButton.isDisplayed();
	System.out.println("Image button is displayed :- "+status);
	
	status = imgButton.isEnabled();
	System.out.println("Image button is enabled :- "+status);
	
	imgButton.click();
	
	//Click on image link
	driver.get("https://selenium.dev/");
	driver.findElement(By.xpath("//body/div[2]/div[1]/div[1]/a[1]")).click();
		
	driver.close();
	}
}

Next

Leave a comment

Design a site like this with WordPress.com
Get started