TypeError:driver.isElementPresent不是函数 [英] TypeError: driver.isElementPresent is not a function

查看:60
本文介绍了TypeError:driver.isElementPresent不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个登录到网站的节点功能,并且无法使其正常工作。我正在尝试等待页面加载使用<$ href =https://stackoverflow.com/questions/25753014/selenium-webdriver中引用的 isElementPresent 函数-wait-till-element-is-displayed>这篇文章但它似乎没有用。

I am attempting to write a node function that logs into a website and am having trouble getting it to work. I am trying to wait for the page to load using the isElementPresent function, referenced in this post but it doesn't seem to be working.

这是我到目前为止所拥有的:

Here's what I have so far:

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不是函数。这里发生了什么,我错过了什么?

When I run the function though I receive the error TypeError: driver.isElementPresent is not a function. What is going on here and what am I missing?

推荐答案

为了与其他Selenium语言绑定保持一致, WebDriver#isElementPresent() WebElement#isElementPresent ()已被弃用。

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

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

If you're using Selenium 3, you should try using findElements instead to determine element present or not as below :-

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

或者如果你想等到欲望元素出现,你应该尝试使用 webdriver.until 如下: -

Or if you want to wait until desire element present, you should try using webdriver.until as below :-

const until = webdriver.until;

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

这篇关于TypeError:driver.isElementPresent不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆