mpen mpen - 2 months ago 19
Javascript Question

Do I always have to wait before clicking?

I'm writing some tests for my site using webdriverjs with mocha/chai, and I find when I do this:

it('select application', function(done) {
client
.click('.disciplinetext > table:nth-child(7) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(3) > button:nth-child(1)')
// more stuff


The element doesn't exist yet. Shouldn't
.click
implicitly wait for the page to finish loading before it actually attempts to click the element?

It works fine if I slap this line before it:

.waitFor('.disciplinetext > table:nth-child(7) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(3) > button:nth-child(1)',1000)


But I don't want to have to throw a
waitFor
before I query I run
.setValue
,
.click
,
.getText
and everything else... am I missing something?

Is there either a command to wait for the whole page to load, or a setting to make implicitly wait before accessing an element?

Answer

The driver.manage().timeouts().implicitlyWait(ms) function does exist in the WebdriverJS Reference API Docs. This API is for the official Javascript API that Selenium supports and @sircapsalot's excerpt comes from. However, it is much cleaner than looking at the Google Code and much easier to navigate.