Select window unable to select Childwindow

In My JavaCode , After clicking on Edit Description link a window get opens (i.e. Java Script Window) Images here 1st one gives anchor tag with attributes and 2nd one is opened window

Image 1:: enter image description here

Image 2:: Window image Along with Page Source here enter image description here

What i need is A. Select the window open(java script) B. Enter the text into Text Area and click Ok.

control is waiting infinite @ Select Window key word its not moving forward. I have to kill Control forcefully.

Here is the code

package Bala.AutoPratice.module1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WindowSwitch {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "D:\\bin\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        //driver.manage().window().maximize();
        driver.get("http://10.1.111.165/Login.aspx");
        //To check if we have landed in the correct place
        driver.findElement(By.id("LoginName")).sendKeys("User1");
        driver.findElement(By.id("Password")).sendKeys("[email protected]");
        driver.findElement(By.id("LoginBtn")).click();

        driver.get("http://10.1.111.165/roles.aspx");
        driver.findElement(By.xpath("//*[@id=\"NewBtn\"]/span")).click();

        driver.findElement(By.xpath("//*[@id=\"aspnetForm\"]/table/tbody/tr[1]/td[2]/a")).click();

        String MainWindow=driver.getWindowHandle();
        Set<String> s1=driver.getWindowHandles();       
        Iterator<String> i1=s1.iterator();      

        while(i1.hasNext())         
        {       
            String ChildWindow=i1.next();       

            if(!MainWindow.equalsIgnoreCase(ChildWindow))           
            {           
                   // Switching to Child window
                    driver.switchTo().window(ChildWindow);  // HERE IT NEVER SELECTS                                                                                                           
                    driver.findElement(By.xpath("//*[@id=\"descript\"]")).sendKeys("[email protected]");                         
                    driver.findElement(By.xpath("//*[@id=\"myform\"]/div/input[1]")).click();           

            // Closing the Child Window.
                        driver.close();     
            }       
        }       
        // Switching to Parent window i.e Main Window.
            driver.switchTo().window(MainWindow);   


        driver.quit();



    }



}

If i use this

Alert myAlert = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
        myAlert.sendKeys("I_am_bbk");
        myAlert.accept();

I am getting this error

INFO: HTTP Status: '404' -> incorrect JSON status mapping for 'no such alert' (400 expected)
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for alert to be present (tried for 10 second(s) with 500 MILLISECONDS interval)
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'HIBAWL56712', ip: '10.158.126.17', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities

Answers:

Answer

It is pretty clear from your snapshot that the window which gets opened is a JavaScript window i.e. an Alert. To enter some text into the Text Area and click Ok you can use the following block of code :

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
//other code
Alert myAlert = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
myAlert.sendKeys("I_am_bbk");
myAlert.accept();
driver.quit();

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.