Selenium Webdriver script to click on link or button

selenium webdriver script to click on link or button

Script1

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
driver.findElement(By.xpath("/html/body/div/div[3]/div/div/div/div/ol/li[3]/a/span[2]")).click();
System.out.println("Successfully clicked on Images link");

}

}

Output will be :-

Successfully clicked on Images link


Above script clicks on Images link in google website.


Script 2

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {

public static void main(String[] args) {

   /*
        Simple script example to
        click on button in google website.
       
   */
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
driver.findElement(By.id("gbqfq")).sendKeys("Selenium");
driver.findElement(By.id("gbqfb")).click();
          //driver.findElement(By.xpath("/html/body/div/div[3]/div/div/div/div/ol/li[3]/a/span[2]")).click();



}

}

Above script enters Selenium in textbox and clicks on google search button.

Single line and Multiline comments in selenium webdriver:-

Singleline comment:-In above script line //driver.findElement(By.xpath("/html/body/div/div[3]/div/div/div/div/ol/li[3]/a/span[2]")).click(); line is comment.
Single line comment starts with // and ends at the end of the line.


Multiline comment:-

/*
        Simple script example to
        click on button in google website.
       
 */

This is a multiline comment which starts with /* and ends with */. All the lines between /* and */ will be treated as comments.



No comments:

Related Posts Plugin for WordPress, Blogger...