0

When I click button submit on that page, it will window.open() a new page with an alert.

Does someone know how to get the new page's alert text "got you ?"

Below is my code, but it does not work.

public static void main(String[] args) throws Exception{
    System.setProperty("webdriver.chrome.driver","D:\\auto\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.yizhike.com.cn/chou/a.html");
    String current = driver.getWindowHandle();
    driver.findElement(By.id("submit")).click();

    Set<String> handlers = driver.getWindowHandles();
    for (String s : handlers){
        if (!s.equals(current)){
            driver = driver.switchTo().window(s);
            String aa = driver.switchTo().alert().getText(); // **it will take endless time in here**
            System.out.println(aa);
        }
    }
}

a.html:

<html>
<head>
    <title>page a</title>
<script type="text/javascript">
  function Wopen(){
      window.open('b.html','_blank','width=600,height=400,top=100px,left=0px')
      } 
</script>
</head>
<body>
<input type="button" onClick="Wopen()" value="submit" id="submit"/>
</body>
</html>

b.html:

<html>
<head>
    <title>page b</title>
<script type="text/javascript">
  alert("got you!");
</script>
</head>
</html>

3 Answers3

0

To extract the text got you! from the Alert which appears on the newly opened window, you can use the following Locator Strategies:

System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver =  new ChromeDriver(options);
driver.get("http://www.yizhike.com.cn/chou/a.html");
String mainWindow = driver.getWindowHandle();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#submit"))).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> allHandles = driver.getWindowHandles();
for(String handle:allHandles) {
    if(!mainWindow.equalsIgnoreCase(handle)) {
        driver.switchTo().window(handle);
        new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
        System.out.println(driver.switchTo().alert().getText());
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You given wrong URL: driver.get("http://www.yizhike.com.cn/chou/b.html") in your code. It should be "http://www.yizhike.com.cn/chou/a.html" (parent window URL).

I have also mentioned code below worked for me:

    WebDriver Driver = new ChromeDriver();
    Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    Driver.manage().window().maximize();
    String url = "http://www.yizhike.com.cn/chou/a.html";
    Driver.get(url);
    String current = Driver.getWindowHandle();
    Driver.findElement(By.id("submit")).click();
    Set<String> handlers = Driver.getWindowHandles();
    Iterator<String> it = handlers.iterator();
    String Parentid = it.next();
    String Childid = it.next();
    System.out.println(Childid);
    Driver.switchTo().window(Childid);*/
    System.out.println(Driver.switchTo().alert().getText());
arpita biswas
  • 144
  • 1
  • 6
0

Am not sure how it works in IE and not in Chrome, but if i can guess the way they consume JavaScript to load the pages may differ

When i try to read the page Source after switching to the new window it was waiting for the page to complete its load.not sure why..

I tried to automate using AUTOIT.

    WebDriverManager.chromedriver().setup();
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.yizhike.com.cn/chou/a.html");
    driver.manage().window().maximize();
    String current = driver.getWindowHandle();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    driver.findElement(By.id("submit")).click();

    Set<String> handlers = driver.getWindowHandles();
    for (String s : handlers) {
        if (!s.equals(current)) {
            driver.switchTo().window(s);
           //calling AutoIT Script to click ok

            Runtime.getRuntime().exec("C:\\Users\\Muthukumar\\Desktop\\pu.exe";     
           //refreshing the page to get the popup again     

            driver.navigate().refresh();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            System.out.println("Alert data: " + alertText);
            alert.accept();

        }
    }
    driver.quit();
*******************************************************
AUTO IT Script:
sleep(4000)
$pos=MouseGetPos()
MouseClick("left",478,252,1,0)
MouseMove($pos[0],$pos[1],0)
mk7644
  • 33
  • 2
  • 9