Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

javascript - Selenium WebDriver wait till element is displayed

I have searched on Google and the SO site and I get answers for JAVA but do not seem to get answers for node.js

I have a web app that takes time to load. I would like the selenium program to wait till the page is loaded and then perform some actions.

My current code is as follows

//dependencies
var webdriver = require('selenium-webdriver'),
    util = require('util'),
    _ = require('underscore');

var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
var branchName =  _.isUndefined(process.argv[3]) ? 'branch' : process.argv[3], 
    hostName = _.isUndefined(process.argv[2]) ? 'localhost' : process.argv[2],
    appTmpl = 'http://%s/%s',
    username = 'xxxx',
    password = 'xxxx';
var appUrl = util.format(appTmpl, hostName, branchName);

driver.get(appUrl);
driver.findElement(webdriver.By.name("username")).sendKeys(username);
driver.findElement(webdriver.By.name("password")).sendKeys(password);
driver.findElement(webdriver.By.name("login_button")).click();
driver.quit();

The error I get is:

    C:Workstudyselenium
ode_modulesselenium-webdriverlibwebdriverpromise.js:1643
      throw error;
            ^
NoSuchElementError: no such element
  (Session info: chrome=37.0.2062.103)
  (Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64)
    at new bot.Error (C:Workstudyselenium
ode_modulesselenium-webdriverlibatomserror.js:109:18)
    at Object.bot.response.checkResponse (C:Workstudyselenium
ode_modulesselenium-webdriverlibatoms
esponse.js:106:9)
    at C:Workstudyselenium
ode_modulesselenium-webdriverlibwebdriverwebdriver.js:277:20
    at C:Workstudyselenium
ode_modulesselenium-webdriverlibgoogase.js:1243:15
    at webdriver.promise.ControlFlow.runInNewFrame_ (C:Workstudyselenium
ode_modulesselenium-webdriverlibwebdriverpromise.js:1539:20)
    at notify (C:Workstudyselenium
ode_modulesselenium-webdriverlibwebdriverpromise.js:362:12)
    at notifyAll (C:Workstudyselenium
ode_modulesselenium-webdriverlibwebdriverpromise.js:331:7)
    at resolve (C:Workstudyselenium
ode_modulesselenium-webdriverlibwebdriverpromise.js:309:7)
    at fulfill (C:Workstudyselenium
ode_modulesselenium-webdriverlibwebdriverpromise.js:429:5)
    at C:Workstudyselenium
ode_modulesselenium-webdriverlibwebdriverpromise.js:1406:10
==== async task ====
WebDriver.findElement(By.name("username"))
    at webdriver.WebDriver.schedule (C:Workstudyselenium
ode_modulesselenium-webdriverlibwebdriverwebdriver.js:268:15)
    at webdriver.WebDriver.findElement (C:Workstudyselenium
ode_modulesselenium-webdriverlibwebdriverwebdriver.js:711:17)
    at Object.<anonymous> (C:Workstudyseleniumest.js:15:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I stumbled upon an answer to my question

So to wait for an element to appear we have to:

driver.wait(function () {
    return driver.isElementPresent(webdriver.By.name("username"));
}, timeout);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...