选择窗口无法选择Childwindow

时间:2018-01-26 04:54:39

标签: javascript java selenium selenium-webdriver

在我的JavaCode中,单击Edit Description链接后会打开一个窗口(即Java Script Window)这里的第一个图像为锚标记提供属性,第二个窗口打开窗口

图片1 :: enter image description here

图像2 ::窗口图像与页面源一起在这里 enter image description here

我需要的是A.选择打开的窗口(java脚本)B。在文本区域中输入文本,然后单击确定。

控制正在等待无限@选择Window关键字它不向前移动。我必须强行杀死控制。

这是代码

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("User1@123");
        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("gadddn@gmail.com");                         
                    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();



    }



}

如果我使用这个

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

我收到此错误

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

1 个答案:

答案 0 :(得分:1)

从您的快照中可以清楚地看到,打开的窗口是 JavaScript 窗口,即警报。要在文本区域中输入一些文本并单击确定,您可以使用以下代码块:

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