Changes

Jump to navigation Jump to search
2,367 bytes added ,  12:19, 20 December 2017
no edit summary
===Selectors===
These functions deal with web elements on the current page the driver is on. Any function that contains find_element_by returns a single web element, and any function that contains find_elements_by returns a list of web elements.
 
driver.find_element_by_class_name(class_name)
This function takes a string class name of the element you're looking for, and finds the first element on the page that has that class name. If there is a possibility that more than one web element on the page has the same class, you are probably better of using find_elements_by_class_name.
 
driver.find_element_by_name(name)
This function takes a string name of the element you're looking for, and finds the first element on the page that has a name attribute matching the string. Similar to the find_element_by_class_name function, this is not your best bet if there are multiple objects with the same name attribute.
 
driver.find_element_by_id(id)
This function takes a string id of the element you're looking for, and finds the element on the page that has an id attribute matching the string. Since ids are guaranteed to be unique, this will always find the element you're looking for. This function is not helpful if the element you want to select does not have an id attribute.
 
driver.find_element_by_xpath(xpath)
This function takes an XPATH, and returns the first web element that matches the path. This should not be your first choice if many elements can share the same XPATH. Contrary to all the above functions, XPATH can be used to find any web element, regardless of its attributes. However, XPATH takes some time to learn, and is more complex than all of the above. After investing some time, XPATH is the most secure way to find the elements you're looking for.
 
driver.find_elements_by_class_name(class_name)
This is similar to find_element_by_class_name, except it returns a list of all matches with the class name. This allows you to iterate over the results or index them accordingly. This is often useful for search results, or any sort of list based queries.
 
driver.find_elements_by_name(name)
Same as find_element_by_name, except it returns a list of all matches.
 
driver.find_elements_by_xpath(xpath)
Same as find_element_by_xpath, but returns a list of all matches.
 
A tutorial on XPATH can be found [https://www.w3schools.com/xml/xpath_intro.asp here].

Navigation menu