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

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();

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.

Related

How to press (Ctrl+T) in selenium-webdriver on Ubuntu (Firefox)?

I want to open a new tab in an existing browser, but in ubuntu(os) with firefox, it's not working.
I have tried both actions and robot class but still, I am unable to do it.
driver.get("http://www.google.com/");
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
System.out.println(driver.getTitle());
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
Also, have tried the below code,
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
driver.manage().Timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
System.out.println(driver.getTitle());
Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();
driver.get("http://www.bing.com/");
System.out.println(driver.getTitle());
act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();
driver.get("http://www.yahoo.com/");
System.out.println(driver.getTitle());
After using both the above code still, a new tab is not getting opened.
Can anyone help me with this??
You could use JavaScript Executor to open a new tab.
((JavascriptExecutor) driver).executeScript("window.open()");
List<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

Selenium: Unable to locate email type box, not within any iframe

I am trying to detect the login id and password field of a website : https://mretailstore.com/login but seems selenium is not able to locate the email type box. I have checked stackoverflow but didn't get any solution to this. Someone has used iframe because of what he/she was facing the same issue but here we have not incorporated any iframe.
The error I am getting is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: .//*[#id='identity']
The code I am using:
System.setProperty("webdriver.gecko.driver", "C:\\Users\\MI SERVICE\\Downloads\\geckodriver.exe");
FirefoxOptions capa = new FirefoxOptions();
capa.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capa);
driver.get("https://www.mretailstore.com/");
driver.findElement(By.xpath(".//*[#id='identity']")).sendKeys("abc#d.com");
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys("abc123");
driver.findElement(By.id("loginbutton")).click();
driver.navigate().back();
driver.close();
It looks your xpath is correct only and this exception is happening before element rendering.So, Please add the some explicit wait after the page loading.
It is working for me with/without Explicit Wait.
Code:
driver.get("https://www.mretailstore.com/");
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.titleIs("Login"));
driver.findElement(By.xpath(".//*[#id='identity']")).sendKeys("abc#d.com");
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys("abc123");
driver.findElement(By.id("loginbutton")).click();

Select a date from Jquery, date picker using selenium webdriver

Go to the Official Website of Jquery https://jqueryui.com/datepicker/
It's not allow be to click on input text even it's have unique id="datepicker" getting an error element not found Exception but when i runs locally by adding jquery date picker it works likes a charm. can somebody help me can't able to figure it out!
2) By using this url i can select anything but it not works with jquery official site as i mentioned above https://jqueryui.com/resources/demos/datepicker/default.html
Below is my actual code that is not getting work
System.setProperty("webdriver.chrome.driver","C:\\ProgramFiles\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://jqueryui.com/datepicker/");
Thread.sleep(5000);
driver.findElement(By.id("datepicker")).click();
The element with id="datepicker" is within a frame. So we have to switch to the intended frame first, then locate the element and then call the click() method as follows:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://jqueryui.com/datepicker/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#class='demo-frame'][#src='/resources/demos/datepicker/default.html']")));
driver.findElement(By.id("datepicker")).click();
System.out.println("Datepicker Clicked");

Can we handle the chrome alert which appears while automating the webpage using selenium?

Please follow the below mentioned steps:
1.) go to this website : http://demo.guru99.com/V4/index.php
2.)punch in any junk login id and password , and then the dialog box appears says:
USER OR PASSWORD IS NOT VALID
3.)Q1: Is there any way I can punch in the "ok" button in this dialog box ???because I can't inspect the element of this dialog box.
4.)Q2: When u try to login to gmail [ password shouldn't be saved], after punching the login details, it asks, do you want to save this password for this site ? Can this popup be handled ? because here also I can't inspect any information .
Q1: Selenium can interact with javascript (browser based) alerts using the Alert interface.
You can try any of the following:
using the explicit wait
Wait<WebDriver> wait = new FluentWait<>(driver).withTimeout(5, TimeUnit.SECONDS).pollingEvery(500,msecs).ignoring(NoSuchElementException.class
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
directly getting the alert
driver.switchTo().alert().accept();
using webdriver / chromedriver capabilities (to handle unexpected alert exception)
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
Q2: I'm unable to replicate the save password popup for gmail. However, you should be able to do it by using .dismiss(); instead of .accept();.
Note: If you're using google chrome for testing, be reminded that chrome usually updates automatically. Selenium alert interaction may not work all of a sudden if it is not compatible with the latest chrome version. With this, I suggest you keep your chromedriver exe and java bindings updated.
Q1: Is there any way I can punch in the "ok" button in this dialog box ???because I can't inspect the element of this dialog box.
Answer: For scenarios where you need to handle an alert, you can write a generic method similar to the one mentioned below, and pass the appropriate arguments while invoking/calling it:
public static void handleAlert(WebDriver driver, boolean accept) {
Alert popup = driver.switchTo().alert();
if (accept)
popup.accept();
else
popup.dismiss();
}
While calling above method, you can pass true, when you want to accept the alert and false when you do not want to accept it.
Q2: When u try to login to gmail [ password shouldn't be saved], after punching the login details, it asks, do you want to save this password for this site ? Can this popup be handled ? because here also I can't inspect any information .
Answer: Solution in this scenario will depend on which browser is being used. As, you are using Chrome browser, you can use following to code to handle the required pop-up:
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches", "--disable-extensions --disable-extensions-file-access-check --disable-extensions-http-throttling --disable-infobars --enable-automation --start-maximized");
options.setExperimentalOption("credentials_enable_service", false);
options.setExperimentalOption("profile.password_manager_enabled", false);
WebDriver driver = new ChromeDriver(options);
Let me know, if you have any further queries or above solutions do not work for you.
Try using JavascriptExecutor to accept alert:
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }");
Alert alert=driver.switchTo().alert();
alert.accept();
You can accept the alert box pop out as follows.
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "C:\\Images";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();

Webelement.Click() - Getting exception on Webelement.Click() though it clicks the Element

Meta
OS: Windows
Selenium Version: 2.53.1.0
IDE: Visual Studio 2013
Browser: Internet Explorer 11 version 11.420
I am getting an exception when i tried to click an element on webpage. This happens when a link is clicked and it opens a dialog. Webelement.click() function clicks the element and the modal dialog is opened but Click() takes time to return and finally logs exception as 'The HTTP request to the remote WebDriver server for URL "" timed out after 60 seconds.'
Expected Behavior:
It should click the "Firefox Beta" download button and "IE tool bar" with RUN and SAVE option will appear
Actual Behavior:
It clicks the "Firefox Beta" download button and "IE tool bar" is coming. But downloadElement.Click() waits for 60 seconds and throws exception.
Steps to reproduce:
Below is code snippet:
string url = "https://www.mozilla.org/en-US/firefox/channel/#beta";
try{
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(url);
Thread.Sleep(5000);
IWebElement downloadElement = driver.FindElement(By.XPath("//div[#id='download-button-desktop-beta']/ul/li/a/strong"));
Thread.Sleep(5000);
downloadElement.Click();
}catch{
//catch block
}
Try giving this xpath instead of that.
IWebElement downloadElement = driver.FindElement(By.XPath("/html/body/div[2]/div/main/section[1]/div/div/ul/li[1]/a/strong"));
Sometimes their is some problems with IE11 selenium is not able to work as expected. so i use double click instead of click in certain scenarios.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("/html/body/div[2]/div/main/section[1]/div/div/ul/li[1]/a/strong"))).doubleClick().perform();
try using both, hope it will helps
You can add implicit wait after downloadElement.Click() and wait for modal dialog to load completely.
Try this. It worked for me -
package sbps;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class cvs_signup
{
public static void main(String[] args) {
String url = "https://www.mozilla.org/en-US/firefox/channel/#beta";
try{
WebDriver driver = new InternetExplorerDriver();
driver.get(url);
Thread.sleep(5000);
WebElement downloadElement = driver.findElement(By.xpath("(//a[#href='https://download.mozilla.org/?product=firefox-beta-stub&os=win&lang=en-US'])[last()]"));
Thread.sleep(5000);
downloadElement.click();
}catch(Exception e){
//catch block
}
}
}