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();
    }

}

4 comments:

  1. Hi Sanjay,
    I tried the same script with different idea. 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 cancel
    8) switch back to frame and click on twitter
    9) close the twitter
    10)switch back to frame and click on your email
    11) close the email window
    12) then close the frame (which has 3 options facebook, twitter and your email)
    13) click on india in main page.

    The code is :

    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.manage().window().maximize();
    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(".//*[@class='rc']/h3/a")).click();
    driver.findElement(By.linkText("Log In")).click(); //click on 'Login' button
    driver.switchTo().frame("signupsso");
    driver.findElement(By.id("fb")).click();
    Iterator it1=driver.getWindowHandles().iterator();
    String times=it1.next();
    String fb=it1.next();
    driver.switchTo().window(fb);
    driver.findElement(By.id("persist_box")).click();
    driver.findElement(By.id("u_0_2")).click();
    driver.switchTo().window(times);
    driver.switchTo().frame("signupsso");
    driver.findElement(By.id("tw")).click();
    Iterator it2=driver.getWindowHandles().iterator();
    String times2=it2.next();
    String tw=it2.next();
    driver.switchTo().window(tw);
    driver.close();
    driver.switchTo().window(times2);
    driver.switchTo().window(times);
    driver.switchTo().frame("signupsso");
    driver.findElement(By.id("tw")).click();
    Iterator it3=driver.getWindowHandles().iterator();
    String times3=it3.next();
    String sso=it3.next();
    driver.switchTo().window(sso);
    driver.close();
    driver.switchTo().window(times3);
    driver.findElement(By.xpath("//*[@id='mainnav']/li[3]/a")).click();
    }
    }

    My question to you is :
    1. Can these steps be done without using Iterator thrice as used by me ? (is there a way to loop)
    2. After 7th step on cancel the window closes, here I switch to the main window. Instead can I use defaultcontent function ? Will the defaultcontent function switch to the frame ?

    ReplyDelete
  2. Hi I am unable to find the frame element in the 1st case.
    can you please help?

    ReplyDelete
  3. right click on frame there you find the option as this frame.

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

    WebDriver driver=new FirefoxDriver();
    driver.get("https://www.google.co.in/?gws_rd=cr&ei=RhKZVfS5HdK7uASjv4KYDw");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//input[@id='lst-ib']")).sendKeys("times of india");
    driver.findElement(By.xpath("//a[contains(text(),'Times of India')]")).click();
    WebDriverWait w=new WebDriverWait(driver, 10);
    w.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//img[@title='The Times of India']")));
    driver.findElement(By.xpath("//span[@class='login']")).click();



    List ls1=driver.findElements(By.xpath("//iframe[@src='http://timesofindia.indiatimes.com/loginview.cms?register=1']"));
    System.out.println(ls1.size());

    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='google_osd_static_frame']")));
    Thread.sleep(5000);
    driver.findElement(By.xpath("//span[@class='fb']")).click();

    ReplyDelete