{"id":15614,"date":"2023-11-25T09:00:00","date_gmt":"2023-11-25T09:00:00","guid":{"rendered":"https:\/\/businessyield.com\/tech\/?p=15614"},"modified":"2023-11-24T23:36:33","modified_gmt":"2023-11-24T23:36:33","slug":"python-selenium-everything-you-need-to-know","status":"publish","type":"post","link":"https:\/\/businessyield.com\/tech\/terms\/python-selenium-everything-you-need-to-know\/","title":{"rendered":"Python Selenium: Everything You Need To Know","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"
Web applications receive new updates and features every few weeks for higher user engagement. Automation testing is necessary to test these features and ensure the UI works well. For Python testers worldwide, Selenium is the first choice for executing automated tests.<\/p>
Python is one of the most popular and on-demand programming languages right now. The reason is obvious; it offers plenty of benefits to its users and is considered an all-rounder. You can create websites using Python by leveraging tools such as Flask, Django, and so on. It is also possible to perform web automation using Selenium. <\/p>
Also, you can use Python for Data Science and Machine Learning, etc. Its simple syntax and the tons of libraries and packages it provides, make it a favorite among programmers.<\/p>
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc. <\/p>
Selenium Tutorial covers all topics such as \u2013 WebDriver, WebElement, Unit Testing with Selenium. <\/p>
The first step for you to start working on Selenium with Python is that you need to write functional test cases using the Selenium web driver. Then, you need to forward a request to the Selenium server that sits at the back end, which will execute the test cases on the browsers automatically. <\/p>
You can perform tests on any browser including Firefox, Chrome, IE, etc. The only thing that changes are the web drivers, which are specific for each browser.<\/p>
Blending Selenium with Python usually works via APIs – which you can use to write functional or acceptance test cases with the help of the Selenium web driver. Using this API, you can easily access all the different types of functionalities in an intuitive manner.\u00a0<\/p>
The first step is to install the Selenium package for Python. You can do so using the simple pip command.<\/p>
$ pip install selenium<\/p> While the installation of Selenium makes the functionality available to you, you need additional drivers for it to be able to interface with a chosen web browser. The download links for the drivers are listed below:<\/p> For the remainder of this Python Selenium tutorial, we will use Chromedriver. Follow the link for the browser of your choice and download the driver for the compatible version.<\/p> If you only plan to test Selenium locally, downloading the package and drivers should suffice. However, if you would like to set Selenium up on a remote server, you would additionally need to install the Selenium Server.\u00a0Selenium Server\u00a0is written in Java; you need JRE 1.6 or above to install it on your server. It is available on Selenium\u2019s\u00a0download page<\/a>.<\/p> Once you have completed the pre-requisites section, you can start your first test in Selenium with the Python programming language!<\/p> 1.<\/strong>\u00a0First, import the WebDriver and Keys classes from Selenium.<\/p> The WebDriver class will connect you to a browser instance. The Keys class lets you emulate the stroke of keyboard keys, including special keys like \u201cShift<\/strong>\u201d and \u201cReturn<\/strong>\u201d.<\/p> 2.<\/strong>\u00a0Next, create an instance of Chrome with the path of the driver you downloaded through the websites of the respective browser. This example assumes the driver is in the same directory as the Python script you will execute.<\/p> If you are testing on your local machine, this opens an instance of Chrome locally. This command lets you perform tests on it until you use the .close()<\/strong> method to end the connection to the browser.<\/p> 3.<\/strong>\u00a0Next, use the\u00a0.get()<\/strong>\u00a0method of the driver to load a website. You may also load a local development site as this process is equivalent to opening a window of Chrome on your local machine, typing a URL, and hitting Enter. The\u00a0.get()<\/strong>\u00a0method starts loading a website and waits for it to render completely before moving on to the next step.<\/p> 4.<\/strong>\u00a0Once the page loads successfully, you can use the\u00a0.title<\/strong>\u00a0attribute to access the textual title of the webpage. If you wish to check whether the title contains a particular substring, use the assert or if statements. For simplicity, let us print the title of the page.<\/p> The output is the following text:<\/p> If you run the test on a Python interpreter, you notice that the Chrome browser window is still active. Also, a message on Chrome states that automated software is currently controlling it.<\/p> 5.<\/strong>\u00a0Next, let us submit a query in the search bar. First, select the element from the HTML DOM, enter a value into it, and submit the form by emulating the Return key press. You can select the element using its CSS class, ID, name attribute, or tag name. If you check the source of the query search bar, you notice that the name attribute of this DOM element is \u201cq\u201d. <\/p> Therefore, you can use the\u00a0.find_element_by_name()<\/strong>\u00a0method as follows to select the element.<\/p> 6.<\/strong>\u00a0Once the DOM element is selected, you first need to clear its contents using the\u00a0.clear()<\/strong>\u00a0method, enter a string as its value using the\u00a0.send_keys()<\/strong>\u00a0method and finally, emulate the press of the Return key using\u00a0Keys.RETURN<\/strong>.<\/p> You notice in the window that these actions trigger a change in the URL with the search results in the window. To confirm the current URL of the window, you can use the following command.<\/p> The following string is displayed:<\/p> To close the current session, use the\u00a0.close()<\/strong>\u00a0method. It also disconnects the link with the browser.<\/p> In this example, we have looked at the steps involved in running our first test using Selenium Python. Do note that we kept the window open during all test stages to ensure you knew what happened in the background as you ran each command.<\/p> In a fully automated flow, you will run multiple tests sequentially and may not be able to view each step as they occur.<\/p> To summarise the discussion, here is your\u00a0first Selenium test on Python<\/strong>. You may save it in the file\u00a0selenium_test.py<\/strong>\u00a0and run python\u00a0selenium_test.py<\/strong>\u00a0to run the test.<\/p> The parent of all the classes and methods that are used for Selenium with Python is the Selenium WebDriver. Using this, you can perform any type of function or operation on any element on a webpage, hence, it is considered as the driving force of Selenium. It has a ton of attributes and methods that are used to aid automation testing in Selenium Python. <\/p>How to run your first test on Selenium with Python<\/strong><\/span><\/h2>
from selenium import webdriver from selenium.\n\nwebdriver.common.keys import Keys<\/code><\/pre>
driver = webdriver.Chrome('.\/chromedriver')<\/code><\/pre>
driver.get(\"https:\/\/www.python.org\")<\/code><\/pre>
print(driver.title)<\/code><\/pre>
Welcome to Python.org<\/code><\/pre>
search_bar = driver.find_element_by_name(\"q\")<\/code><\/pre>
search_bar.clear() \nsearch_bar.send_keys(\"getting started with python\") search_bar.send_keys(Keys.RETURN)<\/code><\/pre>
print(driver.current_url)<\/code><\/pre>
'https:\/\/www.python.org\/search\/?q=getting+started+with+python&submit='<\/code><\/pre>
driver.close()<\/code><\/pre>
from selenium import webdriver \nfrom selenium.webdriver.common.keys import Keys \n\ndriver = webdriver.Chrome('.\/chromedriver') driver.get(\"https:\/\/www.python.org\") \nprint(driver.title) \nsearch_bar = driver.find_element_by_name(\"q\") \nsearch_bar.clear() \nsearch_bar.send_keys(\"getting started with python\") search_bar.send_keys(Keys.RETURN) \nprint(driver.current_url) \ndriver.close()<\/code><\/pre>
Selenium WebDriver Methods<\/strong><\/span><\/h2>
Some of the most frequently used methods for Selenium WebDriver are:<\/span><\/h5>