0

我正在尝试编写一个脚本来导航浏览器上的页面。我需要做的第一件事就是关闭出现的警报。警报关闭后,我需要输入用户名和密码,然后按 OK 按钮。我相信我拥有执行此操作的所有代码,只是没有以正确的顺序发生。当我尝试和控制台记录代码的执行流程时,看起来在警报关闭之前输入了输入用户名和密码的代码。现在我可以让警报关闭,但在那之后什么也没有。我尝试编写一个不成功的异步函数,并且还搜索了使用隐式和显式等待的类似问题,但我也没有运气。

以下是我的代码。我意识到这段代码可能效率低下,但现在我最感兴趣的是让代码正常工作。

// 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 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)

这是警报的图片 这是警报的图片。

4

0 回答 0