英文:
How to retrieve the alert text from the newly opened window using Selenium and Java?
问题
当我在该页面上点击"submit"按钮时,它将使用window.open()
打开一个新页面并显示一个警告。
有人知道如何获取新页面的警告文本"got you?"
以下是我的代码,但它不起作用。
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(); // **在这里会花费无限的时间**
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>
英文:
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>
答案1
得分: 0
从新打开的窗口中提取文本 got you!,出现在 Alert,您可以使用以下 定位策略:
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());
}
}
英文:
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());
}
}
答案2
得分: 0
你在代码中给出了错误的URL:driver.get("http://www.yizhike.com.cn/chou/b.html")。应该是 "http://www.yizhike.com.cn/chou/a.html"(父窗口URL)。
我还在下面提到的代码中提到了对我起作用的代码:
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());
英文:
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());
答案3
得分: 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)
英文:
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)
专注分享java语言的经验与见解,让所有开发者获益!
评论