0

I am having a website which when opened gives a alert box in which you have to enter the username and password to login. how can I do that?

Please click on this link for help!!

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Sammy
  • 25
  • 2
  • 11

2 Answers2

0
Please check with this
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class JavaScriptAlertTest {

public static void main(String[] args) {

    WebDriver myTestDriver = new FirefoxDriver();
    myTestDriver.get("...blablabla....");

    myTestDriver.manage().window().maximize();

    myTestDriver.findElement(By.xpath("//input[@value = 'alert']")).click();

    Alert javascriptAlert = myTestDriver.switchTo().alert();
    System.out.println(javascriptAlert.getText()); // Get text on alert box
    javascriptAlert.accept();

    System.out.println("*************prompt******************************************");

    myTestDriver.findElement(By.xpath("//input[@value = 'prompt']")).click();

    Alert javascriptprompt = myTestDriver.switchTo().alert();
    javascriptprompt.sendKeys("This is Selenium Training");

    System.out.println(javascriptprompt.getText()); // Get text on alert box

    javascriptprompt.accept();
    javascriptprompt = myTestDriver.switchTo().alert();

    System.out.println(javascriptprompt.getText()); // Get text on alert box
    javascriptprompt.accept();

    myTestDriver.findElement(By.xpath("//input[@value = 'prompt']")).click();

    javascriptprompt = myTestDriver.switchTo().alert();

    System.out.println(javascriptprompt.getText()); // Get text on alert box

    javascriptprompt.dismiss();
    javascriptprompt = myTestDriver.switchTo().alert();

    System.out.println(javascriptprompt.getText()); // Get text on alert box
    javascriptprompt.accept();

    System.out.println("***********************************confirm dialog box****************************");
    myTestDriver.findElement(By.xpath("//input[@value = 'confirm']")).click();

    Alert javascriptconfirm = myTestDriver.switchTo().alert();
    javascriptconfirm.accept();

    javascriptconfirm = myTestDriver.switchTo().alert();
    System.out.println(javascriptconfirm.getText()); // Get text on alert box
    javascriptconfirm.accept();

    myTestDriver.findElement(By.xpath("//input[@value = 'confirm']")).click();
    javascriptconfirm = myTestDriver.switchTo().alert();

    javascriptconfirm.dismiss();
    javascriptconfirm = myTestDriver.switchTo().alert();
    System.out.println(javascriptconfirm.getText()); // Get text on alert box
    javascriptconfirm.accept();

}
}
Ref:https://www.seleniumeasy.com/selenium-tutorials/how-to-handle-javascript-alerts-confirmation-prompts
http://stackoverflow.com/questions/8244723/alert-handling-in-selenium-webdriver-selenium-2-with-java
Pradeep
  • 1,947
  • 3
  • 22
  • 45
0

you can use switch frame concept.

you can use this code for switching to child window and then back to parent window.

Code:

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

*****perform operations on child window******************

driver.switchTo().window(parentWindowHandler);//This will switch back to main page after entering username and password.

I hope this will solve your problem

rajan
  • 77
  • 1
  • 7