13 Selenium Commands Every Developer Must Know
Photo by Fotis Fotopoulos on Unsplash In our last tutorial, we discussed why Selenium is the best automation tool and demonstrated how we could run a simple test case in Python. As we said, Selenium is compatible with leading programming languages like Python, Java, Ruby, and others. We can use it with any of our popular browsers like Chrome, Firefox, Edge, and lots more. Today, we’ll discuss the top commands we need to know when working with Selenium in Python. Commands for Searching Specific Web Elements The list of commands we will first discuss is the commands used for automating and searching web pages. They are also called Locators in Selenium. Now, to identify these specific elements on a web page, we will use the findElement() command. Here is the list of the commands; find_element_by_xpath: This can be used to locate and return the first element with the XPath syntax. Xpath can also be implemented when working with an XML document, and mainly XPath is used when the code isn’t working successfully with ID, class, or name. We can implement XPath in our script as shown below; 1 #we import our library 2 from selenium import webdriver 3 4 #here we paste link to our exe file in the webdriver.chrome method 5 driver = webdriver.Chrome(r’C:\Users\CNDRO\Documents\Selenium_project\Browser\chromedriver.exe’) 6 #we maximize our window 7 driver.maximize_window() 8 9 #We will use the driver.get to open the wiki main page 10 driver.get(“https://en.wikipedia.org/wiki/Main_Page”) 11 12 #we copy our xpath of the elements we are interested in from the Inspect element in browser 13 elements = driver.find_element_by_xpath(‘//*[@id=”n-contents”]/a/span’) 14 15 16 print(elements) 17 18 #we then close the browser 19 driver.close() 20 21 2. find_element_by_id: We use the find_element_by_id to return first element with id attribute value matching the location of our item. If no element was found, the NoSuchElementException would be raised. An example is shown below; 1 #we import our library 2 from selenium import webdriver 3 4 #here we paste link to our exe file in the webdriver.chrome method 5 driver = webdriver.Chrome(r’C:\Users\CNDRO\Documents\Selenium_project\Browser\chromedriver.exe’) 6 #we maximize our window 7 driver.maximize_window() 8 #We will use the driver.get to open the wiki main page 9 driver.get(“https://en.wikipedia.org/wiki/Main_Page”) 10 #we specify the id of the element we want to search for here 11 elements = driver.find_element_by_id(‘mw-navigation’) 12 13 print(elements) 14 15 #we then close the browser 16 driver.close() 17 3. find_element_by_name: We can also find our element by name. To do that, we use the find_element_by name locator and pass the name attribute. This will return the first element found during the search. An example is shown below; 1 #we import our library 2 from selenium import webdriver 3 4 #here we paste link to our exe file in the webdriver.chrome method 5 driver = webdriver.Chrome(r’C:\Users\CNDRO\Documents\Selenium_project\Browser\chromedriver.exe’) 6 #we maximize our window 7 driver.maximize_window() 8 #We will use the driver.get to open the wiki main page 9 driver.get(“https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Wikipedia%3AAbout”) 10 #we specify the name of the element we want to search for here 11 elements = driver.find_element_by_name(‘wploginattempt’) 12 13 print(elements) 14 15 #we then close the browser 16 driver.close() 4. find_element_by_class_name: To use the find_element by class name locator, we use it along by specifying the element’s class name. This will give us the first element found in our search. An example is shown below; 1 #we import our library 2 from selenium import webdriver 3 4 #here we paste link to our exe file in the webdriver.chrome method 5 driver = webdriver.Chrome(r’C:\Users\CNDRO\Documents\Selenium_project\Browser\chromedriver.exe’) 6 #we maximize our window 7 driver.maximize_window() 8 #We will use the driver.get to open the wiki main page 9 driver.get(“https://en.wikipedia.org/wiki/Main_Page”) 10 #we specify the class name of the element we want to search for here 11 elements = driver.find_element_by_class_name(‘mw-parser-output’) 12 13 print(elements) 14 15 #we then close the browser 16 driver.close() 17 5. find_element_by_link_text: We use this find_element_by_link_text to find a particular hyperlinked text in a page. To use this locator, we specify the name of the hyperlinked text inside our locator. This will return the first element found in our search. An example is shown below; 1 #we import our library 2 from selenium import webdriver 3 4 #here we paste link to our exe file in the webdriver.chrome method 5 driver = webdriver.Chrome(r’C:\Users\CNDRO\Documents\Selenium_project\Browser\chromedriver.exe’) 6 #we maximize our window 7 driver.maximize_window() 8 #We will use the driver.get to open the wiki main page 9 driver.get(“https://en.wikipedia.org/wiki/Main_Page”) 10 #we specify the name of the element we want to search for here 11 elements = driver.find_element_by_link_text(‘Contents’) 12 13 print(elements) 14 15 #we then close the browser 16 driver.close() 6. find_element_by_css_selector: It is possible to find an element using the CSS selector. We specify the CSS selector we want to use inside the locator during the search. The first element with the matching selector will be returned, and if no element was returned, the NoSuchElementException would be raised. An example is shown below; 1 #we import our library 2 from selenium import webdriver 3 4 #here we paste link to our exe file in the webdriver.chrome method 5 driver = webdriver.Chrome(r’C:\Users\CNDRO\Documents\Selenium_project\Browser\chromedriver.exe’) 6 #we maximize our window 7 driver.maximize_window() 8 #We will use the driver.get to open the wiki main page 9 driver.get(“https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Wikipedia%3AAbout”) 10 #we specify the css selector of the element we want to search for here 11 elements = driver.find_element_by_css_selector(‘input.wploginattempt’) 12 print(elements) 13 14 #we then close the browser 15 driver.close() 7. find_element_by_tag_name: Just as we mentioned the CSS selector earlier, we can also find our element using the Html Tag name. This tag name will be used inside the find_element_by_tag_name locator. This will return the first element that matches the tag name; if no element, a NoSuchElementException will be raised. An example is shown below; 1 #we import our library 2 from selenium import webdriver 3 4 #here we paste link to our exe file in the webdriver.chrome method 5 driver = webdriver.Chrome(r’C:\Users\CNDRO\Documents\Selenium_project\Browser\chromedriver.exe’) 6 #we maximize our window