2

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
Pikamander2
  • 7,332
  • 3
  • 48
  • 69
CodeLover
  • 1,054
  • 6
  • 24
  • 40

2 Answers2

6

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.

Debugging python object/properties

Community
  • 1
  • 1
NickSlash
  • 4,758
  • 3
  • 21
  • 38
  • now according to you when you would use `find_elements_by_tag_name` and get the collections,then how would you iterate over it to get reach to the desired `anchor` element? – CodeLover Dec 30 '12 at 12:13
  • the the edit at the end, my python is poor at best. I would suggest you debug the result from the function and see what you get. – NickSlash Dec 30 '12 at 12:44
  • @NickSlash You may not know Python or Selenium well, but this is absolutely correct. There's one other subtle effect - find_element_by_whatever may involve a long wait if "implicit waits" are enabled and the item doesn't exist. But find_elements_by_whatever will return immediately, with a collection containing 0 elements. – Ross Patterson Dec 30 '12 at 22:14
0

find_element_by_tag_name(tag_name)

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")
    

find_elements_by_tag_name(tag_name)

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")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352