Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Selenium在chrome中获取警报框_Java_Selenium_Google Chrome - Fatal编程技术网

Java 如何使用Selenium在chrome中获取警报框

Java 如何使用Selenium在chrome中获取警报框,java,selenium,google-chrome,Java,Selenium,Google Chrome,我想在窗口打开时得到警报框。我尝试使用Thread.sleep延迟进程,但仍然无法工作 您可以在新的ExpectedConditions.alertIsPresent()上调用WebDriverWait,而不是thread.sleep(),以确保在尝试调用alert.getText()之前已完全加载警报: 如果这对您不起作用,您的浏览器可能不会显示真正的警报——您可以尝试检查弹出窗口,查看其背后是否有HTML元素来验证这一点。您可以使用显式等待,直到警报出现后再切换到它。试试下面的代码 impo

我想在窗口打开时得到警报框。我尝试使用Thread.sleep延迟进程,但仍然无法工作

您可以在新的
ExpectedConditions.alertIsPresent()
上调用
WebDriverWait
,而不是
thread.sleep()
,以确保在尝试调用
alert.getText()
之前已完全加载警报:


如果这对您不起作用,您的浏览器可能不会显示真正的警报——您可以尝试检查弹出窗口,查看其背后是否有HTML元素来验证这一点。

您可以使用显式等待,直到警报出现后再切换到它。试试下面的代码

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
// add the above import statements

// wait for alert to exist
new WebDriverWait(driver, 30).until(ExpectedConditions.alertIsPresent());

Alert alert = driver.switchTo().alert();
String msg = alert.getText();
您确定它是警报框(web警报或窗口警报)还是弹出窗口?
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
// add the above import statements

// wait for alert to exist
new WebDriverWait(driver, 30).until(ExpectedConditions.alertIsPresent());

Alert alert = driver.switchTo().alert();
String msg = alert.getText();
new WebDriverWait(driver, 40)
        .ignoring(NoAlertPresentException.class)
        .until(ExpectedConditions.alertIsPresent());

Alert al = driver.switchTo().alert();

String msg = al.getText();