0

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)

this is a picture of the alert This is the picture of the alert.

Zach
  • 33
  • 7
  • What kind is the alert? Basic auth., proxy, ... Pls post a screenshot. – pburgr Mar 20 '19 at 13:49
  • @pburgr I posted a screen shot of the alert, please let me know if you have any thoughts. Thanks! – Zach Mar 21 '19 at 16:10
  • OK, it is a browser alert. It's reachable by selenium. The aproach with 'driver.wait(until.alertIs...' seems to failing. Your code is logging action before executing them, that's why seems the log to be OK untill failing on the alert. Better to: 1. load the page and wait untill it is loaded (no alert check) 2. make if condition to accept alert (if present) 3. enter credentials. If you can provide url I can try it by myself. – pburgr Mar 22 '19 at 07:27

0 Answers0