问题
Hi I am using Robot() to handle windows pop up. I am getting UnhandledAlertException at
if (driver.getTitle().trim().equalsIgnoreCase(title)).
There are no more open alerts and yet the exception is thrown.
I tried using-
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
But then i get the issue for ignore- InvalidArgumentException
public void VerifyTitle(String title, String URL, String UserId, String Password) throws InterruptedException, IOException{
driver.get(URL);
driver.manage().timeouts().implicitlyWait(50000, TimeUnit.SECONDS);
EnterUserId(UserId);
EnterPassword(Password);
driver.manage().timeouts().implicitlyWait(50000, TimeUnit.SECONDS);
Thread.sleep(1000);
EnterUserId(UserId);
EnterPassword(Password);
driver.manage().timeouts().implicitlyWait(50000, TimeUnit.SECONDS);
Thread.sleep(1000);
EnterUserId(UserId);
EnterPassword(Password);
try{
if (driver.getTitle().trim().equalsIgnoreCase(title)){
System.out.println("PASS");
}else
System.out.println("FAIL");
}catch (Exception e) {
e.printStackTrace();
System.out.println("FAIL");
}
}
public void EnterUserId(String userId) throws IOException{
try{
Robot rb = new Robot();
rb.delay(5000);
StringSelection username = new StringSelection(userId);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
System.out.println("UserId has been entered");
rb.delay(5000);
}catch (Exception e) {
e.printStackTrace();
System.out.println("UserId has not been entered");
}
}
public void EnterPassword(String password) throws IOException{
try {
Robot rb = new Robot();
rb.delay(5000);
StringSelection pwd = new StringSelection(password);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
System.out.println("Password has been entered");
rb.delay(5000);
}catch (Exception e) {
e.printStackTrace();
System.out.println("Password has not been entered");
}
}
And this is how am launching my browser-
@Parameters({"browserName"})
@BeforeTest
public void beforeTest(String browserName){
threadDriver = new ThreadLocal<RemoteWebDriver>();
if(browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
threadDriver.set(new ChromeDriver());
}else {
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
dc.setCapability("acceptInsecureCerts",true);
threadDriver.set(new FirefoxDriver(dc));
}
}
来源:https://stackoverflow.com/questions/45843818/unhandledalertexception-issue-for-windows-pop-up