I'm trying to write a script that will navigate a page on the browser. One of the first things I need to do is close an alert that appears. Once the alert is close I need to enter a user name and password and press an OK button. I believe that I have all the code to do this it is just not happening in the correct order. When I try and console log the flow of the execution of the code it looks like the code that would enter the username and password are entered before the alert is close. Right now I am able to get the alert to close but nothing after that. I have tried writing an async function that was unsuccessful and also searched for similar issues where the use of implicit and explicit waits were used but I have had no luck there as well.
The following is my code that I have. I realize there are probably inefficiencies in this code but for now I am most interested in getting the code to work.
// dependencies
const webdriver = require('selenium-webdriver'),
By = webdriver.By,
Keys = webdriver.Key,
until = webdriver.until;
const chrome = require('selenium-webdriver/chrome');
const path = require('chromedriver').path;
//so I don't have to have path on computer
const service = new chrome.ServiceBuilder(path).build();
chrome.setDefaultService(service);
//allows the use of webdriver
var driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
//enters username, password, hits enter
const enterPassword = () => {
console.log("this is where the password is entered")
driver.wait(until.alertIsPresent(0)).then(() => { driver.switchTo().defaultContent(); });
driver.findElement(By.name('username')).sendKeys('username123');
driver.findElement(By.name('password')).sendKeys('password123');
driver.findElement(By.name('cmdSubmit')).sendKeys(Keys.ENTER);
}
//switches window to alert and accepts it
const closeAlert = () => {
console.log("this is where the alert gets closed")
driver.wait(until.alertIsPresent()).then(() => { driver.switchTo().alert().accept(); });
}
//switches back to main window
const mainWindow = () => {
console.log("this is where we switch back to the main page")
driver.wait(until.alertIsPresent(0)).then(() => { driver.switchTo().defaultContent(); });
// driver.switchTo().defaultContent()
}
const openPortal = () => {
driver.get('examplewebpage.com');
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
openPortal();
closeAlert();
mainWindow();
enterPassword();
This is what my console is out putting :
this is where the alert gets closed
this is where we switch back to the main page
this is where the password is entered
DevTools listening on ws://127.0.0.1:61841/devtools/browser/9b3cc5e1-c320-410e-9b45-66b0d07fb840
(node:908) UnhandledPromiseRejectionWarning: UnexpectedAlertOpenError: unexpected alert open: {Alert text :
You are using a browser other than Microsoft Internet Explorer 5.x (or post versions).
The minimum recommended web browser software is Microsoft Internet Explorer 5.0
Access to features and functionality of this site may be restricted or unavailable.}
(Session info: chrome=72.0.3626.121)
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17134 x86_64)
at Object.checkLegacyResponse (C:\Automation\Everest-Automation\node_modules\selenium-webdriver\lib\error.js:592:13)
at parseHttpResponse (C:\Automation\Everest-Automation\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (C:\Automation\Everest-Automation\node_modules\selenium-webdriver\lib\http.js:468:26)
at process._tickCallback (internal/process/next_tick.js:68:7)