What is the difference between the function with the singular name and the one with the plural name?
find_element_by_name
find_elements_by_name
and
find_element_by_tag_name
find_elements_by_tag_name
What is the difference between the function with the singular name and the one with the plural name?
find_element_by_name
find_elements_by_name
and
find_element_by_tag_name
find_elements_by_tag_name
I'm not all that familiar with python and selenium but most DOM functions act the same way.
find_element_by_name
should return an element who's name attribute matches the name (the first instance found)
<input name="username" type="text" value="Enter Username" />
find_elements_by_name
will return a collection/array of matching elements
<input name="continue" type="submit" value="Login" />
<input name="continue" type="button" value="Clear" />
find_element_by_tag_name
will be similar only returning the first instance of an element with the matching tag name.
find_element_by_tag_name("a") // return the first anchor
find_elements_by_tag_name
will again return a collection/array of matching tag names.
If its possible to chain these commands, or call them on stored elements the result from get_element(s)_*
functions will be relative to node its called on.
<html>
<body>
<div>
<a href="#1">Example 1</a>
<a href="#2">Example 2</a>
</div>
<span>
<a href="#3">Example 3</a>
<a href="#4">Example 4</a>
</span>
</body>
</html>
Example
find_element_by_tag_name("a")
== Example 1
find_element_by_tag_name("span").find_element_by_tag_name("a")
== Example 3
Iteration over collection/array
links = browser.find_elements_by_tag_name("a")
for link in links
# link should be a Selenium WebElement?
if in doubt you can just dump the whole result to see what is in it.
The function with the singular name is find_element_by_tag_name(tag_name)
which finds an element by the tag name and returns the element.
Details:
find_element_by_tag_name(tag_name)
Args :
name - name of html tag (eg: h1, a, span)
Returns :
WebElement - the element if it was found
Raises :
NoSuchElementException - if the element wasn’t found
Usage :
element = driver.find_element_by_tag_name("h1")
The function with the plural name is find_elements_by_tag_name(tag_name)
which find elements by tag name and returns a list.
Details:
find_elements_by_tag_name(tag_name):
Args :
name - name of html tag (eg: h1, a, span)
Returns :
list of WebElements - a list with elements if any was found. An empty list if not
Usage :
elements = driver.find_elements_by_tag_name("h1")