How to handle iframe in selenium webdriver using java.

iFrame:
An iFrame (Inline Frame) is an HTML document embedded inside the current HTML document on a website. iFrame HTML element is used to insert content from another source, such as an advertisement, into a Web page. A Web designer can change an iFrame's content without making them reload the complete website. A website can have multiple frames on a single page.



 iFram Tutorial

  
    

Alert Box

Alert Box

Click the button to display an alert box.


Step 1: To switch inside a frame

Switch to Frames by Index:
Index of an iFrame is the position at which it occurs in the HTML page. In the above example we have found total number of iFrames. In the sample page we have two IFrames, index of iFrame starts from 0. So there are two iFrames on the page with index 0 and 1.
driver.switchTo().frame(int iframnumber);//pass frame number as parameter.
driver.switchTo().frame(0);
Switch to Frames by Name:
Now if you take a look at the HTMLcode of iFrame you will find that it has Name attribute. Name attribute has a value iframe_name1.
driver.switchTo().frame(String frameName); //pass frame name as parameter.
driver.switchTo().frame("iframe_name1");

Switch to Frame by ID:
Similar to the name attribute in the iFrame tag we also have the ID attribute. We can use that also to switch to the frame.
driver.switchTo().frame(String Id); //pass frame id as parameter.
driver.switchTo().frame("iFram_id1");
Switch to Frame by Web Element:
iFrame by simply passing the iFrame WebElement to the driver.switchTo().frame() command. First find the iFrame element using any of the locator strategies and then passing it to switchTo command
driver.switchTo().frame(WebElement locator ); 
driver.switchTo().frame(driver.findElement(By.id("iFram_id1")));


Step 2: After switching inside a frame selenium will be able to operate on elements.
driver.findElement(By.partialLinkText("Results")).click();
driver.findElement(By.xpath("/html/body/div[6]/div[2]/div[1]/section[1]/div[1]/ul/li[2]/a")).click();

Step 3: Switching back to Main page from Frame
There is one very important command that will help us to get back to the main page. Main page is the page in which two iFrames are embedded. Once you are done with all the task in a particular iFrame you can switch back to the main page using the switchTo().defaultContent().
driver.switchTo().defaultContent();

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


public class iFram {

    public static void main(String[] args) throws InterruptedException {

// Initialize Firefox Web driver   
WebDriver driver = new FirefoxDriver();
//Maximize browser window   
driver.manage().window().maximize();
//Go to Page  
driver.get("file:///C:/Users/Hiro%20Mia/Desktop/iFrame.html");

//Switch by Index
driver.switchTo().frame(0);
//operate web elements on iFram.
driver.findElement(By.xpath("/html/body/div[6]/div[2]/div[1]/section[1]/div[1]/ul/li[2]/a")).click();

//Switch back to the main window
driver.switchTo().defaultContent();
//operate web elements on Main window. 
driver.findElement(By.xpath("/html/body/fieldset/button")).click();
//handle to the open  confirmation popup.  
Alert Confirm = driver.switchTo().alert();
Thread.sleep(3000);
//click on Cancel button.         
Confirm.dismiss();
//close browser         
driver.quit();
    }

}


How to resize or maximize current browser window and get window size in Selenium Webdriver using java.

Get current window size
 WebDriver driver = new FirefoxDriver();
 driver.manage().window().getSize();
Resize current browser window on width and height
WebDriver driver = new FirefoxDriver();
Dimension dimension=new Dimension(500, 600);
driver.manage().window().setSize(dimension);
Move browser window to specific point base on x and y coordinate
 WebDriver driver = new FirefoxDriver();
Point point=new Point(100, 100)
driver.manage().window().setPosition(point);
Maximize current window size
 WebDriver driver = new FirefoxDriver();
 driver.manage().window().maximize();
OR
WebDriver driver = new FirefoxDriver();
Toolkit toolkit = Toolkit.getDefaultToolkit();
int Width = (int) toolkit.getScreenSize().getWidth();
int Height = (int) toolkit.getScreenSize().getHeight();
Dimension screenResolution = new Dimension(Width, Height);
driver.manage().window().setSize(screenResolution);

Example:
import java.awt.Toolkit;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Resizewindow {

public static void main(String args[]) throws InterruptedException {
// Initialize firefox  driver      
WebDriver dr = new FirefoxDriver();

//Get current window size
System.out.println(dr.manage().window().getSize());

// Resize browser window base on width and height
dr.manage().window().setSize(new Dimension(500, 600));
System.out.println(dr.manage().window().getSize());
Thread.sleep(4000);

//Move browser window to specific point base on x and y coordinate
dr.manage().window().setPosition(new Point(20, 50));
System.out.println(dr.manage().window().getSize());
Thread.sleep(4000);

//Maximize browser window       
dr.manage().window().maximize();
System.out.println(dr.manage().window().getSize());
Thread.sleep(4000);

dr.manage().window().setPosition(new Point(0, 0));
dr.manage().window().setSize(new Dimension(500, 600));
Thread.sleep(4000);

//Maximize browser window  
Toolkit toolkit = Toolkit.getDefaultToolkit();
int Width = (int) toolkit.getScreenSize().getWidth();
int Height = (int) toolkit.getScreenSize().getHeight();
Dimension screenResolution = new Dimension(Width, Height);
dr.manage().window().setSize(screenResolution);
System.out.println(dr.manage().window().getSize());

//close firefox browser  
dr.quit();
    }

}

How to get all valid urls from a website in selenium webdriver using java.

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
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 static selenium.practice.BdJobsQARelatdJobsHtmlUnitDriver.m;


public class Getallurlsfromwebsite {

    static WebDriver driver;

    static List<WebElement> mainurl, suburl;
    static List<String> uniqueurl;

    public static void main(String[] a) throws InterruptedException {
        // Initialize Firefox driver 
        driver = new FirefoxDriver();
        //Maximize browser window 
        driver.manage().window().maximize();

        String websiteaddress = "http://www.google.com";
        //Go to website  
        uniqueurl = new ArrayList();
        driver.get(websiteaddress);
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //get all url of page
        mainurl = driver.findElements(By.tagName("a"));

for (int k = 0; k < mainurl.size(); k++) {
            // check url not null, empty and only current website 
            if (!(mainurl.get(k).getText().trim().equals("")) && !(mainurl.get(k).getText().trim() == null) && !(mainurl.get(k).getAttribute("href").trim() == null) && !(mainurl.get(k).getAttribute("href").trim().equals("")) && (mainurl.get(k).getAttribute("href").contains("google.com")) && !(mainurl.get(k).getAttribute("href").contains("@"))) {

       if (!(uniqueurl.contains(mainurl.get(k).getAttribute("href").trim()))) {
   uniqueurl.add(mainurl.get(k).getAttribute("href").trim());
//Print urls
 System.out.println(mainurl.get(k).getAttribute("href"));

                    // Open new tab
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
  driver.get(mainurl.get(k).getAttribute("href").trim());
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// get all sub page url
     suburl = driver.findElements(By.tagName("a"));

      /get all sub page url
      for (int m = 0; m < suburl.size(); m++) {
        if (!(suburl.get(m).getText().trim().equals("")) && !(suburl.get(m).getText().trim() == null) && !(suburl.get(m).getAttribute("href").equals("")) && !(suburl.get(m).getAttribute("href") == null)) {

     if (!(uniqueurl.contains(suburl.get(m).getAttribute("href").trim()))) {
  uniqueurl.add(suburl.get(k).getAttribute("href").trim());
       //Print urls
System.out.println(suburl.get(m).getAttribute("href"));
                            }

                        }
                    }
  // close open tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
// Move default window
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t");
driver.switchTo().defaultContent();
                    Thread.sleep(7000);

                }
            }
        }
        driver.quit();
    }

}


How to handle mouse actions in selenium webdrver usnig java

Selenium Webdriver mouse actions as run as follow.
void click(WebElement onElement)/void click(Coordinates where)
void contextClick(WebElement onElement)/void contextClick(Coordinates where)
void doubleClick(WebElement onElement)/void doubleClick(Coordinates where)
void mouseDown(WebElement onElement)/void mouseDown(Coordinates where)
void mouseUp(WebElement onElement)/void mouseUp(Coordinates where)
void mouseMove(WebElement toElement)/void mouseMove(Coordinates where)
void mouseMove(WebElement toElement, long xOffset, long yOffset)


Method Name: click
Syntax: click(WebElement onElement), click(Coordinates where)
Description: Similar to the existing click() method. It can also perform a click based on coordinates.
Example:
 Actions actions = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu"));

 actions.click(mainmenu).perform();
or
WebElement mainmenu=drive.findElement(By.id("main_menu"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.click(locat.getCoordinates());


Method Name: contextClick
Syntax: contextClick(WebElement onElement), contextClick(Coordinates where)
Description:  Performs a context click/right-click on an element or based on the coordinates
Example:
 Actions actions = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu"));
actions.contextClick(mainmenu).perform();
action.contextClick(mainmenu).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).perform(); 
or
WebElement mainmenu=drive.findElement(By.id("main_menu"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.
contextClick(locat.getCoordinates());

Method Name: doubleClick
Syntax: doubleClick(WebElement onElement),doubleClick(Coordinates where)
Description:  Performs a double-click on the webelement or based on the coordinates. If left empty, it performs double-click on the current location
Example:
Actions action = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu"));
action.doubleClick(mainmenu).perform();
or
WebElement mainmenu=drive.findElement(By.id("main_menu"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.
doubleClick(locat.getCoordinates());

Method Name: mouseDown
Syntax: mouseDown(WebElement onElement), mouseDown(Coordinates where)
Description: Performs a mouse-down action on an element or based on coordinates.
Example:
Actions action = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu"));
action.mouseDown(mainmenu).perform();
or
WebElement mainmenu=drive.findElement(By.id("sub_menu"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(locat.getCoordinates());


Method Name: mouseUp
Syntax: mouseUp(WebElement onElement),mouseUp(Coordinates where)
Description: Releases the mouse usually followed by mouse-down and acts based on co-ordinates.
Example:
Actions action = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu2"));
action.mouseUp(mainmenu).perform();
or
WebElement mainmenu=drive.findElement(By.id("sub_menu2"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(locat.getCoordinates());


Method Name: mouseMove
Syntax: mouseMove (WebElement onElement),mouseMove (Coordinates where)
Description: Performs a mouse-move action on an element or based on coordinates.
Example:
Actions action = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu"));
action.mouseMove(mainmenu).perform();
or
WebElement mainmenu=drive.findElement(By.id("sub_menu"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.
mouseMove(locat.getCoordinates());

How to get X,Y Coordinates Of element in selenium webdriver using java.


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

public class GetcoordinatesOfelement {

    public static void main(String args[]) {
        // Initialize driver      
        WebDriver dr = new FirefoxDriver();
        //Maximize browser window       
        dr.manage().window().maximize();
        //Go to URL      
        dr.get("http://www.google.com");
        //Set  timeout      
        dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  
   //Locate element for which you wants to retrieve x y coordinates.
   WebElement element = dr.findElement(By.partialLinkText("Gmail"));
  
       //Used points class to get x and y coordinates of element.
        Point point = element.getLocation();
        int xcord = point.getX();
        int ycord = point.getY();
        System.out.println(xcord + ", " + ycord);
  
        //close firefox browser  
        dr.close();
    }

}