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 Introduction to Selenium Automation Nation XPath review

Jan Lundeen
Jan Lundeen
5,881 Points

Xpath review quiz-Question on "Given the HTML code, how many elements would the following call return?"

On the Introduction to Selenium class, on of the Xpath review quiz (after Fork in the Xpath section), gives students some HTML code and asks how many elements would be found using a particular code string [starts with driver.findElement(By.xpath]. I'm not sure why this line of code would only find one instance of "sample" instead of two. I'm not sure if it's because the command used is findElement instead of findElements. Can you clear this up for me?

Thanks,

Jan

1 Answer

Steven Parker
Steven Parker
229,744 Points

You're quite right, those are different functions. The findElement (singular) returns just one element, if there are more that match it returns the first one. And it causes an error if cannot find one.

On the other hand, findElements (plural) returns a list of all the elements that match. If none are found, it returns an empty list with no error.

Jan Lundeen
Jan Lundeen
5,881 Points

So it sounds like that's why the answer is 1 rather than 2. Thanks Steven!