Nivi
Nivi

Reputation: 63

Exception in thread "main" org.openqa.selenium.NoAlertPresentException: no alert open

Using Selenium Webdriver,I tried to open fb page after logging in. Once I logged in ,there is a pop up box appearing as follows "Show notifications
Allow and Block" I want to select "Allow" button.

I got an error message as

Exception in thread "main" org.openqa.selenium.NoAlertPresentException: no alert open

Please help.

Here is the code I wrote:

System.setProperty("webdriver.chrome.driver","C:\\Users\\ABCD\\Desktop\\chromedRiver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.facebook.com");      
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys("xxx");
driver.findElement(By.xpath(".//*[@id='pass']")).sendKeys("xxx");
driver.findElement(By.xpath(".//*[@id='u_0_m']")).click();
Thread.sleep(2000);
Alert alert=driver.switchTo().alert();
String msg= alert.getText();
System.out.println(msg);
Thread.sleep(2000);
alert.accept();

Upvotes: 1

Views: 4158

Answers (1)

iomv
iomv

Reputation: 2707

It is not an alert as stated by Zach, it is a browser notification, and in your case you can press the allow button just by sending the Space Key using the Robot class in Java:

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);

So the code would be:

System.setProperty("webdriver.chrome.driver","C:\\Users\\ABCD\\Desktop\\chromedRiver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.facebook.com");      
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys("xxx");
driver.findElement(By.xpath(".//*[@id='pass']")).sendKeys("xxx");
driver.findElement(By.xpath(".//*[@id='u_0_m']")).click();
    Thread.sleep(4000);
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_SPACE);
    robot.keyRelease(KeyEvent.VK_SPACE);
Thread.sleep(2000);

I am not sure if there is a way to retrieve the notification message though as you were trying to do.

Upvotes: 1

Related Questions