我试着把它放在代码中。
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();
}
}
这是屏幕截图。在这里,您可以查看警告框。我想弹出这个警报窗口。我试了好几次,但还没有用。
答案 0 :(得分:0)
做这样的事情:
在您尝试切换到警报之前,它需要存在。
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();
}
}