【问题标题】:ask for user input for selenium要求用户输入硒
【发布时间】:2019-10-20 15:14:13
【问题描述】:

我正在尝试在 nodejs 中使用 selenium 来自动化我的工作流程。访问 Sellercentral.amazon.com 时,它会向我的手机发送一个 OTP 代码。如何在 nodejs 上要求提示以便输入代码?

我尝试过使用 readline-sync,但提示总是在 selenium 启动之前显示。

const webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

const driver = new webdriver.Builder()
    .forBrowser('firefox')
    // .setFirefoxOptions(options)
    .build();

//Main body
driver.get('https://sellercentral.amazon.com');
driver.wait(until.elementLocated(By.id('sign-in-button')));
driver.findElement(By.id('sign-in-button')).click();

const fillForm = (idToLook, keys) => {
    this.idToLook = idToLook;
    if (keys) {
        driver.wait(until.elementLocated(By.id(idToLook)));
        driver.findElement(By.id(idToLook)).sendKeys(keys);
    }
    else {
        keys = readline.question(`what are the keys for ${this.idToLook}: `);
        driver.findElement(By.id(idToLook)).sendKeys(keys);
    }
}

fillForm('ap_email', amazon.id);
fillForm('ap_password', amazon.password);
driver.findElement(By.name('rememberMe')).click();
driver.findElement(By.id('a-autoid-0')).click();
driver.wait(until.elementIsNotVisible(By.id('auth-mfa-optcode')));

// fillForm('auth-mfa-otpcode');

driver.findElement(By.id('auth-mfa-remember-device')).click();


driver.quit();

【问题讨论】:

    标签: javascript selenium


    【解决方案1】:

    你可以尝试类似的方式。包装 initializeSite 以将站点作为 Promise 启动。

    如下所示的基本脚本:

    
    
    
    function main() {
    
        var initializeSite = initialize();
    
        initializeSite.then(function(result) {
    
    
    
    
    
            // Do your different actions to bring up the form that need OTP
    
           readline.question (“Add OTP”, (otp) =>{
    
           // Add rest of your codes here
          }
    
            console.log(“success”)
    
        }, function(err) {
    
            console.log(err);
    
        })
    
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多