javascript - 选择窗口无法选择子窗口

标签 javascript java selenium selenium-webdriver

在我的 JavaCode 中,单击“编辑描述”链接后,将打开一个窗口(即 Java 脚本窗口),此处的图像第一个给出带有属性的 anchor 标记,第二个打开窗口

图片 1::enter image description here

图像 2::窗口图像以及此处的页面源 enter image description here

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

控件正在无限等待@选择窗口关键字,它不会向前移动。我必须用力杀死Control。

这是代码

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

最佳答案

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

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

关于javascript - 选择窗口无法选择子窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48455986/

相关文章:

java - 太空侵略者游戏

Java 编译错误缺少 return 语句。需要澄清

java - 当某个 WebElement 条件为 true 时如何单击动态按钮

forms - 在不填写表格的情况下在 Selenium 中发出 POST 请求?

javascript - 如何从 google 脚本将值传递到 html 页面

javascript - VSCode : Snippet - Transform characters to another equal length line

Java:OOP概念、抽象类、接口(interface)

javascript - 如何在用户单击某个项目后隐藏下拉菜单?

javascript - 想要同时匹配大写和小写字母

java - 根据电子表格选择页面上的值 - Java - Selenium Webdriver