How to upload files using selenium webdriver


package selenium_examples;


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.files.com/");
driver.manage().window().maximize();
WebElement uploadElement = driver.findElement(By.name("file_0"));
   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
uploadElement.sendKeys("E:\\index.jpg");

 
}
}


 
upload file in selenium webdriver


How to scroll down the page through Selenium Web Driver

Page Scroll down script using selenium webdriver

package selenium_examples;


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


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/preferences?hl=en#languages");
driver.findElement(By.linkText("Edit")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 300)");

 

}
}


Page Scroll up script using selenium webdriver

package selenium_examples;


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


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/preferences?hl=en#languages");
driver.findElement(By.linkText("Edit")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(300, 0)");
 

}
}



 



 

How to right click on a link and open link in new tab using selenium webdriver

Script1:-

package selenium_examples;

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;
import org.openqa.selenium.interactions.Actions;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement glink = driver.findElement(By.linkText("About"));
                                Actions action= new Actions(driver);
action.contextClick(glink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();


}
}


 
The above script clicks on 'About' link in google page and opens 'About google' page in new tab.

right click on link in webdriver


contextClick()-Performs a context-click at the current mouse location.

contextClick(WebElement onElement)-Performs a context-click at middle of the given element

Script2:-

Below script clicks on 'About' link in google page and opens 'About google' page in new window.

package selenium_examples;



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;
import org.openqa.selenium.interactions.Actions;

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

WebElement glink = driver.findElement(By.linkText("About"));
                Actions action= new Actions(driver);
action.contextClick(glink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();


}
}


 

How to highlight an element using selenium webdriver

Script to highlight element border

Script1:-

package selenium_examples;


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


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
       driver.get("http://www.seleniumhq.org/");
       driver.manage().window().maximize();
      WebElement element = driver.findElement(By.name("q"));
       JavascriptExecutor js = (JavascriptExecutor)driver;
       js.executeScript("arguments[0].style.border='5px solid blue'", element);
       }
}

Above script highlights search textbox border in www.seleniumhq.org website.

highlight element in selenium webdriver



Script2:-

package selenium_examples;


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


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
       driver.get("https://www.google.co.in/");
       driver.manage().window().maximize();
      WebElement element = driver.findElement(By.name("btnK"));
       JavascriptExecutor js = (JavascriptExecutor)driver;
       js.executeScript("arguments[0].style.border='5px solid red'", element);
       }
}



 


How to get x and y coordinates of an element using Selenium Webdriver

package selenium_examples;


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

public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
System.out.println("X Position= "+ driver.findElement(By.name("btnK")).getLocation().getX());
System.out.println("Y Position= "+ driver.findElement(By.name("btnK")).getLocation().getY());

}
}


Output:-

X Position= 373
Y Position= 386


How to get Height and Width of an image or element using Selenium Webdriver


Selenium webdriver script to get Width and Height of an Image

Script1:-

package selenium_examples;


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

public class Sample {

public static void main(String[] args)
{


WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
System.out.println("Height of Mercury Tours image= "+ driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/p[1]/img")).getSize().getHeight());
System.out.println("Width of Mercury Tours image= "+ driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/p[1]/img")).getSize().getWidth());

}
}


Output:-

Height of Mercury Tours image= 110
Width of Mercury Tours image= 100


element height and width in webdriver


Script2:-

Script to get Height and Width of google search button

package selenium_examples;


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

public class Sample {

public static void main(String[] args)
{


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
System.out.println("Height= "+ driver.findElement(By.name("btnK")).getSize().getHeight());
System.out.println("Width= "+ driver.findElement(By.name("btnK")).getSize().getWidth());

}
}


Output:-

Height= 29
Width= 102


How to find total number of checkboxes in a page using selenium webdriver

package selenium_examples;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Sample {

public static void main(String[] args)
{

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/preferences?hl=en#languages");
driver.findElement(By.linkText("Edit")).click();

       Listcheckboxes=driver.findElements(By.xpath("//input[@type='checkbox']"));
System.out.println("Total Checkboxes ="+checkboxes.size());

}
}




Output :-

Total Checkboxes =46


checkboxes count webdriver selenium


How to extract number of links and text of links of a page using Selenium WebDriver

package selenium_examples;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {
   public static void main(String[] args)
   {
   
    
            WebDriver driver = new FirefoxDriver();
    
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    
            driver.get("http://newtours.demoaut.com/");
    
            List linkElements = driver.findElements(By.tagName("a"));
    
            System.out.println("Total number of Links ="+linkElements.size());
     
            for(int i=0; i< linkElements.size(); i++)

            {
            System.out.println(linkElements.get(i).getText());
            }
    
       }       
                             }

Output:-             
Total number of Links =16

Home
Flights
Hotels
Car Rentals
Cruises
Destinations
Vacations
SIGN-ON
REGISTER
SUPPORT
CONTACT
your destination
featured vacation destinations
Register here
Business Travel @ About.com
Salon Travel
 


  
 

Handling Alerts or Popups using Selenium webdriver

package selenium_examples;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {
   public static void main(String[] args)
  
                        {
   
  WebDriver driver = new FirefoxDriver();
                driver.get("http://www.rediff.com");
                driver.findElement(By.linkText("Sign in")).click();
                driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
                driver.findElement(By.id("btn_login")).click();
                Alert alt=driver.switchTo().alert();
               System.out.println(alt.getText());
               alt.accept();
              
     
    }
  }

Output:-
Please enter your email ID

Handling alerts or popups in webdriver


Alert alt=driver.switchTo().alert();->Get a handle to the open alert, prompt or confirmation
alt.getText()->Gets the text of alert
alt.accept()->To Accept any alert, i.e Clicking OK button
alt.dismiss()->To dismiss any alert, i.e Clicking Cancel button



  

How to open a new tab using selenium webdriver


package selenium_examples;


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



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://www.google.com");

     driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
   
     System.out.println("New Tab Opened");
   
     driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"w");
   
     System.out.println("New Tab Closed");

}
  }


Output:-

New Tab Opened
New Tab Closed



Script to go back to first tab i,e google.com page

package selenium_examples;


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



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://www.google.com");

     driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
   
     System.out.println("New Tab Opened");
   
     driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL , Keys.TAB);
   
 
}
  }

How to select multiple options from a list box using selenium webdriver


package selenium_examples;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://jobsearch.naukri.com/mynaukri/mn_newsmartsearch.php");

 Select droplist = new Select(driver.findElement(By.id("idqi")));

       droplist.selectByVisibleText("Accounting / Finance");
       droplist.selectByVisibleText("Agriculture / Dairy");
 
}
  }


Above script selects Accounting / Finance and Agriculture / Dairy from Industry list box.

selenium scripts,webdriver scripts



How to deselect selected options from a list box using selenium webdriver

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://jobsearch.naukri.com/mynaukri/mn_newsmartsearch.php");

 Select droplist = new Select(driver.findElement(By.id("idqi")));

       droplist.selectByVisibleText("Accounting / Finance");
       droplist.selectByVisibleText("Agriculture / Dairy");
       droplist.deselectAll();
       System.out.println("Deselected selected options");
}
  }


 deselectAll() method - Clears all selected options.


Webdriver Scripts : How to check whether button is enabled or disabled in Selenium Web driver


package selenium_examples;



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



public class spicejet {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");

if(driver.findElement(By.id("gbqfba")).isEnabled())
{
System.out.println("Button is Enabled");
}
else
{
System.out.println("Button is Disabled");
}


}
  }


Output:-

Button is Enabled


Selenium webdriver script to check whether textbox is enabled or disabled

package selenium_examples;



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



public class spicejet {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");

if(driver.findElement(By.id("gbqfq")).isEnabled())
{
System.out.println("Textbox is Enabled");
}
else
{
System.out.println("Textbox is Disabled");
}


}
  }


Output:-

Textbox is Enabled



Selenium Scripts:How to check whether a list box or dropdown box allows multiple selection or not in selenium webdriver


Script 1 :-

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://jobsearch.naukri.com/mynaukri/mn_newsmartsearch.php");

 Select droplist = new Select(driver.findElement(By.id("idqi")));

      if (droplist.isMultiple())
      {
      System.out.println("Multiple selection is allowed");
      }
   
      else
      {
      System.out.println("Multiple selection is not allowed");
      }
     
      }
  }


Output:-

Multiple selection is allowed

isMultiple() method returns TRUE if the drop-down element allows multiple selection at a time else returns FALSE.


Script 2:-

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;



public class spicejet {

public static void main(String[] args) {


 WebDriver driver = new FirefoxDriver();

 driver.get("http://book.spicejet.com/");

 Select droplist = new Select(driver.findElement(By.id("ControlGroupSearchView_AvailabilitySearchInputSearchVieworiginStation1")));

      if (droplist.isMultiple())
      {
      System.out.println("Multiple selection is allowed");
      }
            else
      {
      System.out.println("Multiple selection is not allowed");
      }
     
      }
  }

Output:-

Multiple selection is not allowed


Selenium Scripts : How to resize current browser window in Selenium WebDriver

Resize the browser window in Selenium Webdriver

Script 1:-

package selenium_examples;



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




public class spicejet {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://funandknowledge.blogspot.com");
Dimension D = new Dimension(800,500);
driver.manage().window().setSize(D);
      }
  }
Selenium Scripts,Resize Browser


Script 2:-

package selenium_examples;



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




public class spicejet {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
Dimension D = new Dimension(500,800);
driver.manage().window().setSize(D);
      }
  }




How to check selected value in dropdown list using selenium webdriver

Script To check selected value in dropdown list using selenium webdriver

package selenium_examples;



import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;




public class spicejet {

public static void main(String[] args) {


 

WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
driver.manage().timeouts().implicitlyWait(100,TimeUnit.SECONDS);
    Select droplist = new Select(driver.findElement(By.name("fromPort")));

        droplist.selectByIndex(3);
        System.out.println(droplist.getFirstSelectedOption().getText());
        System.out.println("Done!");
      }
  }

Output:-

New York
Done!

Selenium webdriver script to check selected value in dropdown list


Selenium Webdriver Scripts - WebDriver.Navigation



package selenium_examples;

import java.util.concurrent.TimeUnit;

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


public class spicejet {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.google.com");
driver.findElement(By.id("gbqfbb")).click();
driver.navigate().back();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.navigate().forward();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.navigate().refresh();
System.out.println("done!");

}
}

navigate.back() - Performs browser back button action
navigate().forward() - Performs browser forward button action
navigate.refresh() - It refreshes the current page
navigate.refresh() - Navigates to particular URL



Mouse hover action in selenium Webdriver


package selenium_examples;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class spicejet {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.get("http://spicejet.com/");

Actions builder = new Actions(driver);
WebElement Element = driver.findElement(By.xpath("html/body/div[2]/div[1]/div/div[2]/div[2]/div/ul/li[4]/a"));
builder.moveToElement(Element).build().perform();

driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);

driver.findElement(By.xpath("/html/body/div[2]/div/div/div[2]/div[2]/div/ul/li[4]/ul/li/a")).click();

System.out.println("Done!");

}
}

Above script opens http://spicejet.com/ website and hovers over menu item "Travel Tips" and clicks "FAQ's".

selenium scripts




How to count the number of drop down values present in a dropdown list using selenium webdriver

package selenium_examples;


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class spicejet {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
driver.manage().timeouts().implicitlyWait(100,TimeUnit.SECONDS);

WebElement we = driver.findElement(By.name("fromPort"));

java.util.List options =  we.findElements(By.tagName("option"));

System.out.println("Dropdown values count = " + options.size());

driver.close();


}
}

Output :-

Dropdown values count = 10

selenium webdriver scripts

Above script logins into Mercury tours website and gets count of number of drop down values present in "Departing From" dropdown list.






Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge