13 Selenium Commands Every Developer Must Know - CNDRO Website
3252
post-template-default,single,single-post,postid-3252,single-format-standard,bridge-core-2.9.4,qode-page-transition-enabled,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-child-theme-ver-1.0.0,qode-theme-ver-27.8,qode-theme-bridge,qode_header_in_grid,wpb-js-composer js-comp-ver-6.7.0,vc_responsive,elementor-default,elementor-kit-2634

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;

  1. 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

 

2find_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
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 tag name of the element we want to search for here
11 elements = driver.find_elements_by_tag_name('div')
12
13 print(elements)
14
15 #we then close the browser
16 driver.close()

 

8. find_element_by_partial_link_text: The find_element_by_partial_link_text is used to grab first element which has link text value we specified in our locator. If no element matches what we specified in our locator, 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
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 tag name of the element we want to search for here
11 elements = driver.find_elements_by_tag_name('Wiki')
12
13 print(elements)
14
15 #we then close the browser
16 driver.close()

 

The Navigational Selenium Commands

Another commands you must know as a Developer while using Selenium are the basic navigation commands.

9. Navigate Forward Command

1 driver.navigate().forward();

10. Navigate To command

1 driver.navigate().to("https://en.wikipedia.org/wiki/Main_Page");

11. Navigate Back Command

1 driver.navigate().back();

12. Navigate Refresh Command

1 driver.navigate().refresh();

Selenium Commands for Handling Frames

The next command to discuss is how we handle frames in Selenium. In our web pages, all our elements are not usually in the same place, and it is possible to have elements under different frames or iframes. We might have to move to where the frames are located to locate a particular element before we start using the commands for searching web Elements we discussed earlier.

13. We can use any of these commands to handle frames;

driver.switch_to_frame("frameid")
driver.switch_to_frame("framename")
driver.switch_to_frame("frameclassname")

 

Thanks for reading. See you in our next post.

No Comments

Post A Comment

AllEscort