Showing posts with label how to handle frame ?. Show all posts
Showing posts with label how to handle frame ?. Show all posts

Tuesday, June 3, 2014

Switch to window then frame then again switch back to frame and window. (complex example)

Note- Please do the manual steps first and see how the flow going then try with the script.
Manual Steps-
1) open google
2) type in search box 'times of india'
3) click on 1st link of 'times of india'
4) click on 'click here to go to timesofindia.com'
5) click on 'Login' button (which is in the top just beside the LogIn with Facebook)
6) click on facebook
7) check the checkbox in facebook page and click on cancle
8) then close the frame (which has 3 options facebook, twitter and your email)
9) click on india in main page.
10) close the browser.

import java.util.Iterator;
import java.util.concurrent.TimeUnit;

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


public class TimesOfIndia {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
        driver.get("https://www.google.co.in/"); //open google
        driver.findElement(By.id("gbqfq")).sendKeys("times of india"); //type in search box 'times of india'
        driver.findElement(By.xpath("//span[text()='times of india']")).click(); //click on 1st link of 'times of india'
        driver.findElement(By.xpath("//div[div[div[div[cite[b[text()='indiatimes']]]]]]//a[text()='The ']")).click(); //click on 'click here to go to timesofindia.com'
        driver.findElement(By.linkText("Log In")).click(); //click on 'Login' button
        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='signupsso']"))); //now switch to login frame which has 3 options facebook, twitter and your email
        driver.findElement(By.id("fb")).click(); //click on facebook which will open in new window
        Iterator<String> it = driver.getWindowHandles().iterator();// get the address of all the windows
        String times = it.next();
        String fb = it.next();
        driver.switchTo().window(fb); // switch to facebook window
        driver.findElement(By.xpath("//input[@type='checkbox']")).click(); //check the checkbox in facebook window
        driver.findElement(By.xpath("//input[@value='Cancel']")).click(); //click on cancle button in facebook window
        driver.switchTo().window(times); //switch back to main window
        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='signupsso']"))); //again switch to login frame which has 3 options facebook, twitter and your email
        Thread.sleep(2000);
        driver.findElement(By.xpath("//img[@src='/photo/11509366.cms']")).click(); // click on cross sign to close frame
        Thread.sleep(2000);
        driver.switchTo().defaultContent(); //switch back to main page
        driver.findElement(By.xpath("//div[ul[script[text()='sectionid=4719157']]]//a[@href='/india/indiaarticlelist/-2128936835.cms']")).click(); //click on india
        driver.close();
    }

}

switch to window then frame.

Note- This is to login for HDFC credit card.

import java.util.Iterator;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

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

public class HdfcBank {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("give your hdfc credit card userid: ");
        String userid = in.next();
        System.out.println("give your pass: ");
        String pass = in.next();
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://www.hdfcbank.com/");
        driver.findElement(By.xpath("//img[@src='/assets/images/login_butn.png']")).click();
        Iterator<String> browser = driver.getWindowHandles().iterator();
        String parent = browser.next();
        String child1 = browser.next();
     
        driver.switchTo().window(child1);
        driver.findElement(By.xpath("//img[@src='/assets/images/netbanking_continue_red.gif']")).click();
        driver.switchTo().frame(driver.findElement(By.xpath("//frame[@src='RSLogin.html']")));
        driver.findElement(By.xpath("//p[strong[text()='Credit Cardholders']]//a[text()='click here']")).click();
     
        Iterator<String> browser1 = driver.getWindowHandles().iterator();
        String child2=null;
        while(browser1.hasNext())
        {
             child2 = browser1.next();
        }
        driver.switchTo().window(child2);
        driver.findElement(By.xpath("//input[@name='fldLoginUserId']")).sendKeys(userid);
        driver.findElement(By.xpath("//input[@type='password']")).sendKeys(pass);
        driver.findElement(By.xpath("//img[@alt='Log In']")).click();
    }

}

Wednesday, May 21, 2014

how to handle frame ?



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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Frame {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
//driver.manage().window().maximize();
driver.navigate().to("https://www.zoho.com/crm/lp/login.html");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement ifr = driver.findElement(By.xpath("//iframe[contains(@src,'accounts.zoho.com/login')]"));
driver.switchTo().frame(ifr);
driver.findElement(By.id("lid")).sendKeys("venkyin.2218@gmail.com");
driver.findElement(By.id("pwd")).sendKeys("Venkatesh");
driver.findElement(By.id("submit_but")).click();
driver.switchTo().defaultContent();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@id='tab_Campaigns']")));
driver.findElement(By.xpath("//a[@id='tab_Campaigns']")).click();
driver.findElement(By.xpath("//tr[td[3][a[text()='Venky']]]")).click();
driver.findElement(By.linkText("Add Existing Leads")).click();
driver.findElement(By.name("search")).click();
driver.close();
}
}