Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Quality Assurance Introduction to Selenium Automation Nation XPath review

Jan Lundeen
Jan Lundeen
5,731 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
218,692 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,731 Points

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