1

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();
2
  • It is not a native alert must be an iframe. You need to switch to the frame and click the option you want Commented Jun 17, 2016 at 14:34
  • It is actually a browser notification, it is not part of the DOM Commented Jun 17, 2016 at 15:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.