7

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)

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?

Community
  • 1
  • 1
jmreicha
  • 3,155
  • 7
  • 32
  • 39

2 Answers2

18

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

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

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);
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • but how can I replace this my code for selenium 3? `driver.wait(function () { return driver.isElementPresent(By.css(".form-control")); }, 10000);` – Rafael C. Dec 14 '16 at 03:31
  • @Saurabh Gaur driver.findElements(By.id('email')).then(found => !!found.length); fails with an error if email is not present in DOM. How can I avoid it? – user911 Jun 12 '18 at 02:55
  • @SaurabhGaur https://stackoverflow.com/questions/50806848/check-if-element-is-present-in-protractor – user911 Jun 12 '18 at 06:33
  • 1
    @user911 actually you are trying with `findElement` while I'm suggesting it using `findElements`...us it instead.. it would return either list of element or empty list..thanks..:) – Saurabh Gaur Jun 12 '18 at 08:37
  • 'if(driver.findElements(By.id('email')).size()>0){ //do something}' - can be used as well. – Dhamo Nov 14 '18 at 22:14
1

Make sure that all of the dependencies like Selenium and the browser driver are installed and working using a very minimal example.

Then you can check what functions are on the driver object with console.log(util.inspect(driver));

If your dependencies are set up, it may be that the API changed slightly. The current example here https://www.npmjs.com/package/selenium-webdriver uses until. You may want to try something closer to that example first.

Jason Livesay
  • 6,317
  • 3
  • 25
  • 31
  • I have verified dependencies have been installed. Probably the API changed? I can't seem to find any reference in the docs, http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index.html. Safe to say this function is no longer used? – jmreicha Sep 28 '16 at 03:05
  • Yes I guess it is no longer used. Maybe elementLocated from 'until' https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/lib/until.js – Jason Livesay Sep 28 '16 at 03:41
  • until works, sort of. It seems to fail intermittently but I think that issue is outside of this question. – jmreicha Sep 29 '16 at 04:47