Search On Google

Saturday, February 16, 2019

Selenium click on href link and image

How to click on a href link by selenium webdriver?

We mostly use linkText and xpath locator for click on href link.
WebElement temp  =    driver.findElement(By.linkText("Click to me")).click();

How to click on image by selenium webdriver?

In image click we mostly use xpath and cssSelector locators.

WebElement temp = driver.findElement(By.xpath("//img[@src='demo1.jpeg']"));
WebElement temp = driver.findElement(By.cssSelector("a[src='demo2.jpeg']"));


Java code example for href click and image click


"package pkg1;

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

public class MyClass8_Click_On_Image { 
    public static void main(String[] args) {       
   
        System.setProperty(""webdriver.chrome.driver"",""C:\\Users\\Desk-105\\Documents\\SELENIUM\\chromedriver.exe"");   
        WebDriver driver = new ChromeDriver();   

        driver.get("https://www.onlinetutorial.info/p/selenium-click-on-link-and-image.html");   
      /**
       * for click on image
       */
        WebElement element = driver.findElement(By.cssSelector(""img[src='a.jpg']""));
        element.click();
     
        /**
         * for click on href link
         */
        driver.findElement(By.linkText(""Click to me"")).click();
     
    }
}

No comments:

Post a Comment

About Me

My photo
Mumbai, Maharashtra, India