选择窗口无法选择Childwindow [英] Select window unable to select Childwindow
问题描述
在My JavaCode中,单击Edit Description链接后会打开一个窗口(即Java Script Window)这里的第一个图像为锚标记提供属性,第二个打开窗口
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
图片1 ::
图片2 ::窗口图片以及页面来源此处
< a href =https://i.stack.imgur.com/mJPf8.png =nofollow noreferrer>
Image 2:: Window image Along with Page Source here
我需要的是A.选择打开的窗口(java脚本)B。在文本区域中输入文本并单击好的。
What i need is A. Select the window open(java script) B. Enter the text into Text Area and click Ok.
控制等待无限@选择窗口关键字它不向前移动。我必须强行杀死Control。
control is waiting infinite @ Select Window key word its not moving forward. I have to kill Control forcefully.
这是代码
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 窗口,即警报。要在文本区域中输入一些文本并单击确定,您可以使用以下代码块:
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();
这篇关于选择窗口无法选择Childwindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!