So How to click on web link, image and tab on webpage using Selenium WebDriver.
Steps :
·         Define Firefox Browser and open the Firefox Browser
·         Open the URL (Website)
·         Get on IMAGE LINK like button using By.xpath
·         Get on WEBLINK using By.linktext  
·         Get on TAB LINK using By.cssSelector
·         Verify Correct Webpage is opened using click on TAB LINK and print on console
·         Verify Correct Webpage is not opened using click on TAB LINK and print on console.
Source Code : 
Image_Link_using_XPATH - driver.findElement(By.xpath(".//*@id='choice']/tbody/tr/td[2]/center/a[1]/img")).click();
                   
                   
                   
                   
                   
                   
                   
                   
Image_Link_using_XPATH - driver.findElement(By.xpath(".//*@id='choice']/tbody/tr/td[2]/center/a[1]/img")).click();
WebLink using Linktext -                   
     driver.findElement(By.linkText("the documentation")).click();
TAB Link using cssSelector -                                     
     driver.findElement(By.cssSelector("#header ul a")).click();
Practice Yourself :
package WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import static org.junit.Assert.assertTrue;
public class Click_on_Link_Image_Tab {
          public static void main(String[] args) throws InterruptedException {
                   //Define Firefox Browser as webdriver and open the Firefox Browser
                   WebDriver driver = new FirefoxDriver();
                   //Open the website (URL)
                   driver.get("http://seleniumhq.org");
                   //Click on IMAGE LINK using cssselector
             driver.findElement(By.xpath(".//*@id='choice']/tbody/tr/td[2]/center/a[1]/img")).click();
                   //Click on WEBLINK using linkText
                   driver.findElement(By.linkText("the documentation")).click();
                   //Click on TAB LINK using ID
                   driver.findElement(By.cssSelector("#header ul a")).click();
                   try {
                             // Verify Correct Webpage is opened using click on TAB LINK
                             assertTrue(driver.getPageSource().contains("About Selenium"));
                             //Message is printed when webpage is opened using click on TAB LINK
                             System.out.println("Webpages are correctly opened");
                   }
                   catch (Throwable e)
                   {
                             //Message is printed when webpage is opened using click on TAB LINK
                             System.out.println("Webpages are NOT correctly opened");
                   }
                   // Close the Firefox Browser
                   driver.close();
          }
}


Nice post.Thanks for sharing best selenium training institute in chennai
ReplyDeleteTraining with placement in chennai