我试图编写一个登录到网站的 Node 功能,但无法使其正常工作。我正在尝试使用this post中引用的isElementPresent函数加载页面,但似乎无法正常工作。

这是我到目前为止的内容:

const webdriver = require('selenium-webdriver')
const By = webdriver.By

var username = ''
var password = ''
var timeout = 5000

function FacebookLogin(username, password) {

    var driver = new webdriver.Builder()
        .withCapabilities(webdriver.Capabilities.chrome())
        .build()

    driver.get('http://www.facebook.com')

    driver.wait(function() {
        return driver.isElementPresent(By.id('email'))
    }, timeout)

    var user = driver.findElement(By.id('email'))
    user.sendKeys(username)

    var pass = driver.findElement(By.id('pass'))
    pass.sendKeys(password)

    pass.submit()
    driver.sleep(5000)
    driver.quit()
}

FacebookLogin(username, password)

当我运行函数时,我收到错误TypeError: driver.isElementPresent is not a function。这是怎么回事,我想念什么?

最佳答案

For consistency with the other Selenium language bindings, WebDriver#isElementPresent() and WebElement#isElementPresent() have been deprecated.

如果您使用的是Selenium 3,则应尝试使用findElements来确定元素是否存在,如下所示:-

driver.findElements(By.id('email')).then(found => !!found.length);

或者,如果您要等到欲望元素出现,则应尝试使用 webdriver.until ,如下所示:-
const until = webdriver.until;

var user = driver.wait(until.elementLocated(By.id('email')), timeout);
user.sendKeys(username);

10-06 15:30