In this article, we will discuss browser commands in selenium with python.
As you know selenium supports many languages. We can write selenium with Java, Javascript, Python, C#, Ruby, etc. All the methods will be same but there will be syntax differences between the languages that you use. Other than that the commands, names will be mostly similar to all languages. If you learned the concepts of selenium using anyone language strongly it is easier to learn and implement the test automation in all the languages as well.
Come let's see the commands one by one.
Prerequisites
- Create a project in pycharm IDE.
- Selenium installed.( Else, you can — pip install selenium)
Install webdriver-manager by running the below command in command prompt.
pip install webdriver-manager
Note
While setting the python interpreter, kindly refer the python.exe file path. This path will available in your appdata folder when you download python. All the libraries that you download using pip command will get installed inside the scripts folder.
C:\User\<username>\Appdata\Local\programs\python\python<version> folder(for me python310)\Scripts
Eg of the file path: C:\User\<username>\Appdata\Local\programs\python\python<version> folder(for me python310)\python.exe
Screenshot
If you do not want to refer local interperter setting like this way, then you can directly install the necessary libraries inside the project that you created and refer the scripts folder in the interpreter settings dialog box.
C:\Users\Username\PycharmProjects\yourprojectname\venv\Scripts\python.exe
For Chrome browser, import the ChromeDriverManager and create the driver object as shown in the code snippet.
A Service class that is responsible for the starting and stopping of chromedriver.
We don't need to download the chromedriver.exe file and provide the path. when we run the test for the first time, the latest version of chromedriver gets downloaded and saved.
And it is used every time when we run test.
When your browser is updated the respective browser driver will automatically download and updated.
# importing the webdriver library
from selenium import webdriver
# importing service class
from selenium.webdriver.chrome.service import Service as ChromeService
# import the ChromeDriverManager
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get("https://test.automationbro.com/");
The below command opens the browser with the desired webpage.
driver.get("<url of the application under test>")
The below command navigates backward. Equivalent to forward button in the browser.
driver.back();
The below command navigates backward. Equivalent to back button in the browser.
driver.forward();
The below command refresh the browser.
driver.refresh();
The below command maximizes the window in the browser
driver.maximize_window();
The below command minimizes the window in the browser.
driver.minimize_window();
Cick command
The below command locates the webelement using the locator and click the element.
element = driver.find_element_by_id("test")
element.click();
8 types of locators are supported which are mentioned below
- By CSS ID: driver.find_element_by_id
- By CSS class name: driver.find_element_by_class_name
- By name attribute: driver.find_element_by_name
- By DOM structure or Xpath: driver.find_element_by_xpath
- by tagName: driver.find_element_by_tag_name()
- By link text: driver.find_element_by_link_text
- By partial link text: driver.find_element_by_partial_link_text
- By HTML tag name: driver.find_element_by_tag_name
Fill command
The below command locates the webelement using the locator and enter input inside the input field.
element = driver.find_element_by_id("testing-id")
element.send_keys("test text")
The below command close the current browser instance.
driver.close();
The below command closes all the browser instances opened.
driver.quit();
The below command gets the title of the page.
titleofwindow=driver.title;
#print the title in the console
print(titleofwindow)
The below command gets the current URL of the page.
currenturl = driver.current_url;
#prints the currenturl in the console
print(currenturl)
Happy learning.