Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Quality Assurance

Pete Lower
Pete Lower
17,225 Points

How to test whether an element exists in the DOM w/ Selenium (node: selenium-webdriver)?

How can I check if an element exists on the page w/o my script failing? isDisplayed only takes in a tag name. Thanks!

2 Answers

Can you provide more context for the element you're trying to find? Specifically, do you expect that the element should exist on the page? Also, are you using WebDriverWait to create a wait for that element to be visible or clickable?

Sounds like you are trying to wait for an element that loads slightly after the page does. You'll probably want to look at something like this which waits until the condition is satisfied:

 driver.get('http://www.google.com/ncr');

 var query = driver.wait(until.elementLocated(By.name('q')));
 query.sendKeys('webdriver\n');

 driver.wait(until.titleIs('webdriver - Google Search'));

Source: