我试着把它放在代码中。
driver.findElement(By.xpath("//*[@id='adduser']/div/form/div[2]/div/div/label")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
String alertMessage = driver.switchTo().alert().getText();
System.out.println(alertMessage);
alert.accept();
我发现了“没有警报”之类的错误消息。
完整的 Java 代码如下..
package AddUser;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class CheckFirstname
{
public static WebDriver d;
public static void main(String []args)throws Exception{
System.setProperty("webdriver.firefox.marionette", "pathToGeckodriver");
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://54.68.159.204/qmsadm");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.name("username"));
element.sendKeys("user name");
element = driver.findElement(By.name("password"));
element.sendKeys("password");
element.submit();
element = driver.findElement(By.linkText("Admin"));
element.click();
element = driver.findElement(By.linkText("Users"));
element.click();
element = driver.findElement(By.xpath("//*[@id='search-container']/a/i"));
element.click();
element = driver.findElement(By.id("username"));
element.sendKeys("#!#!#$@#!$@!$@#$%#%^#$^^&%&$%*");
driver.findElement(By.xpath("//*[@id='adduser']/div/form/div[2]/div/div/label")).click();
Thread.sleep(1000);
WebDriverWait wait = new WebDriverWait(driver, 0);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
}
这是屏幕截图。在这里您可以检查一个警告框。我想弹出这个警告窗口。我试了几次,但还是不行。
最佳答案
做这样的事情:
在您尝试切换到警报之前,它需要存在。
driver.findElement(By.xpath("//*[@id='adduser']/div/form/div[2]/div/div/label")).click(); Thread.sleep(1000);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alertText = alert.getText();
alert.accept();
以及为什么您会在这里收到警报 Alert alert=driver.switchTo().alert();
如果您没有在下面的行中使用 String alertMessage=driver.switchTo().alert().getText();
,你只是在写它是不必要的。希望能帮助到你。
完整的java代码如下。
package AddUser;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class CheckFirstname
{
public static WebDriver d;
public static void main(String []args)throws Exception{
System.setProperty("webdriver.firefox.marionette","pathToGeckodriver");
FirefoxDriver driver=new FirefoxDriver();
driver.get("http://54.68.159.204/qmsadm");
driver.manage().window().maximize();
WebElement element=driver.findElement(By.name("username"));
element.sendKeys("geosony");
element=driver.findElement(By.name("password"));
element.sendKeys("1");
element.submit();
element=driver.findElement(By.linkText("Admin"));
element.click();
element=driver.findElement(By.linkText("Users"));
element.click();
element=driver.findElement(By.xpath("//*[@id='search-container']/a/i"));
element.click();
element= driver.findElement(By.id("username"));
element.sendKeys("#!#!#$@#!$@!$@#$%#%^#$^^&%&$%*");
driver.findElement(By.xpath("//*[@id='adduser']/div/form/div[2]/div/div/label")).click();
Thread.sleep(1000);
WebDriverWait wait = new WebDriverWait(driver, 30 );
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
}
}
关于java - 为什么不弹出警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39385950/