5

Steps that I have to perform

  1. I have a website (www.xyz.com)
  2. I successfully opened it through Firefox.
  3. In the page, there is a google icon (image) which I am suppose to click
  4. After clicking the google icon, a pop up to enter user name and password appears

I am able to click icon but am not able to get the pop up executed through the selenium code.

It stops at clicking the icon only.

I want the pop up to be opened through my code.

I am using window handles to handle pop up.

Can some one help me in this?

I am trying below steps:

1 Go to http://www.rentedroofs.com/dashboard/#/welcome

2 Now try to click Fb or google and try to work on the pop up .

    WebElement element1 = driver.findElement(By.xpath("//html/body/div[3]/md-dialog/md-dialog-content/div/div/div/button[1]"));
    element1.click();
    String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
    String subWindowHandler = null;

    Set<String> handles = driver.getWindowHandles(); // get all window handles
    Iterator<String> iterator = handles.iterator();
    while (iterator.hasNext()){
        subWindowHandler = iterator.next();
    }
    driver.switchTo().window(subWindowHandler); // switch to popup window

I tried -

driver.get("http://www.rentedroofs.com/");
          WebElement element = driver.findElement(By.xpath("/html/body/section[2]/header/div/div/div/div/a"));
          element.click();
         Thread.sleep(10000);

    String handle= driver.getWindowHandle();

    System.out.println(handle);
    WebElement element1 = driver.findElement(By.xpath("//html/body/div[3]/md-dialog/md-dialog-content/div/div/div/button[1]"));
    element1.click();
    Set handles = driver.getWindowHandles();

    System.out.println(handles);
    for (String handle1 : driver.getWindowHandles()) {

        System.out.println(handle1);

        driver.switchTo().window(handle1);

        }

But this is not opening the FB pop UP.

3
  • Can you share your script? Commented Aug 30, 2016 at 7:02
  • what pop-up you are not able to handle? Shivali, Pls share text and screen if possible Commented Aug 30, 2016 at 7:58
  • As I explained in below answer, after launching your provided URL it shows Modal dialog box. It's simple to handle modal dialog box. Please simply modify the code by locating elements of FB/google login button. I think it works. Try it & let me know.
    – Bharat Mane
    Commented Sep 1, 2016 at 10:11

5 Answers 5

4

Before handling any pop up you have to find that, whether it is iFrame/Modal dialog box etc. If it's iframe try to switch over that with locating elements of that iframe.

In your scenario-take a example of twitter Login application:

After click on Login button: It displays Pop up which is called as Modal dialog box.

If you want to confirm for this- Right click on pop up it gives an ID with name as- Modaldialogbox for handling Modal dialogbox, Simply you have to add WAIT before locating elements on that pop up.

public class TwitterLogin 
{

    static WebDriver driver;
    static String baseUrl="https://twitter.com/";

    public static void main(String[] args) throws InterruptedException 
    {
        driver= new FirefoxDriver();
        driver.get(baseUrl);
        driver.manage().window().maximize();

        WebElement LoginButton= driver.findElement(By.xpath("//button[@class='Button StreamsLogin js-login']"));
        LoginButton.click();

        Thread.sleep(10000);
        WebElement UsernameTextbox=    driver.findElement(By.xpath("//input[contains(@name,'email]')]"));
        UsernameTextbox.clear();
        UsernameTextbox.sendKeys("abcd");

        WebElement PasswdTextbox=driver.findElement(By.xpath("//input[contains(@type,'password')]"));
        PasswdTextbox.clear();
        PasswdTextbox.sendKeys("1234");

        PasswdTextbox.submit();     
    }


}
1

It's hard to know what happen without HTML. Please share HTML of your site.

If you want to login your site using google/facebook account, so you can workaround as below

  1. Open google/facebook and login
  2. Open your site and click on google/facebook icon --> it should login immediately without popup alert.
4
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Kate Paulk
    Commented Aug 30, 2016 at 11:43
  • I wish you can see the original post, I answered this one for version 1 of the question. I just provide my idea to help him overcome the issue based on my experience. I think workaround I post above will work for most case. Anyway, thanks for your review.
    – Tam Minh
    Commented Aug 31, 2016 at 3:19
  • The user asked how to handle the popup. Your answer states how to avoid the popup. That is why you have not answered the question and your response should be a comment. If the original version of the question did not ask how to handle the popup, then I suggest you edit your answer to account for this.
    – Kate Paulk
    Commented Aug 31, 2016 at 11:35
  • okay, my bad. I always find the easy to go, maybe he need to check single sign-on function. Thanks for explanation.
    – Tam Minh
    Commented Aug 31, 2016 at 15:43
1

Login for twitter is not a real browser popup, so it is possible to locate it via XPath.

WebElement picture = driver.findElement(By.xpath("//img"));
picture.click();

you have to put there full xpath or ID's etc. it depends on your HTML. Picture could be represented by styled div, so I cannot give more exact solution unless the html code is revealed.

If you application throws a new window popup you can handle it by

   handles = driver.getWindowHandles()
   iterator = handles.iterator()
   WindowHandlerID = iterator.next()
   driver.switchTo().window(WindowHandlerID)

note that this is not the code... code itself could be more complex to protect against exceptions.

If your browser throws an alert popup, there are functions to handle it:

Alert alert = driver.switchTo().alert();
alert.accept();
//* or *//
alert.dismiss();
0

Please follow following steps:

  1. First locate your icon with xpath

  2. Click on that icon

  3. Give some wait after performing this action.

0

Problem is there are two window. First is original and second is sign in related.

First verify title and then perform login.

Run below code and it is working fine for Google+

//open URL
driver.get("http://www.rentedroofs.com/");
Thread.sleep(3000);
//Move to login page
driver.findElement(By.xpath("//a[@href='dashboard']")).click();
Thread.sleep(3000);
//Click on Google+ button
driver.findElement(By.xpath("//*[@md-svg-src='images/icons/google.svg']")).click();
Thread.sleep(3000);
// Store the current window handle
String winHandleBefore = driver.getWindowHandle();


// Switch to new window opened
for (String winHandle : driver.getWindowHandles())
{
    driver.switchTo().window(winHandle);
    System.out.println("Window switch");
    System.out.println(driver.getTitle());
    if (driver.getTitle().contains("Sign in"))
    {
        Thread.sleep(3000);
        driver.findElement(By.id("Email")).sendKeys("[email protected]");
        System.out.println("Email found");
        driver.findElement(By.id("next")).click();
        System.out.println("Next found");
        Thread.sleep(2000);
        driver.findElement(By.id("Passwd")).sendKeys("xxxxxxx");
        System.out.println("Password found");
        Thread.sleep(3000);
        driver.findElement(By.id("signIn")).click();
        Thread.sleep(1000);
    }
}

// Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);

For Facebook change the element as per under :

//Click on Facebook button
driver.findElement(By.xpath("//*[@md-svg-src='images/icons/facebook.svg']")).click();

And code is :

if (driver.getTitle().contains("Facebook"))
{
    Thread.sleep(3000);
    driver.findElement(By.id("email")).sendKeys("[email protected]");
    System.out.println("Email found");
    Thread.sleep(2000);
    driver.findElement(By.id("pass")).sendKeys("xxxxxxxx");
    System.out.println("Password found");
    Thread.sleep(3000);
    driver.findElement(By.name("login")).click();
    Thread.sleep(1000);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.