The web elements on the page need to be accessed with findElement() and findElements() methods in order to perform actions on them. However there are dissimilarities between them, they are listed below −
The findElement method is used to work with one matching web element at a time. The findElements method is used to work with a list of matching elements.
If there is no matching element found on the page, findElement throws a NoSuchElementException whereas an empty list is returned if there are no matching elements on the page.
driver.find_element_by_xpath("//table/tbody/tr[2]/td[1]")
driver.find_elements_by_xpath("//input[@type='text']")
Code Implementation with findElements method.
from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke actual browser driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm") #to refresh the browser driver.refresh() # identifying the edit boxes with type attribute in a list edt =driver.find_elements_by_xpath("//input[@type='text']") # len method is used to get the size of that list print(len(edt)) #to close the browser driver.close()
Code Implementation with findElement method.
from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke actual browser driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm") #to refresh the browser driver.refresh() # identifying the edit boxes with findElement then using send_keys method driver.find_element_by_xpath("//input[@name='firstname']") .send_keys("Selenium") driver.close()