{"id":7621,"date":"2023-11-06T11:35:24","date_gmt":"2023-11-06T06:05:24","guid":{"rendered":"https:\/\/python-programs.com\/?p=7621"},"modified":"2023-11-10T12:19:34","modified_gmt":"2023-11-10T06:49:34","slug":"how-to-install-selenium-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-install-selenium-in-python\/","title":{"rendered":"How to install Selenium in Python | Installing Selenium WebDriver Using Python and Chrome"},"content":{"rendered":"
In this chapter, we will learn how to prepare our system with the required software so that we are able to write our test automation scripts and execute them. We will note to have Python, Java Runtime Engine (JRE), Eclipse IDE, PyDev. After setting up this software for creating scripts, we will need to set up Selenium in the system and download the browser drivers for execution on the individual drivers.<\/p>\n
Structure<\/strong><\/p>\n Objective<\/strong><\/p>\n This chapter will help us to understand the steps to setup Selenium and Eclipse IDE on the Windows system<\/p>\n Installation and setting up<\/strong><\/p>\n Before we start to learn to write scripts to automate browsers using Selenium WebDriver, we have to prepare our system with a few software. We will require the following:<\/p>\n After we have installed and set up the above software in our system, we need to install PyDev in the Eclipse environment. To setup PyDev in the Eclipse environment, we have to perform the following actions:<\/p>\n The following screenshot shows the same:<\/p>\n <\/p>\n Now the Eclipse environment is ready to create Python projects and understand Python syntax as well. The next step is to install the Selenium Python module.<\/p>\n Installing Selenium Python module<\/strong><\/p>\n The modules of Python are available on the website (http:\/\/www. pypi.org). From here, we will search the module for Selenium and use the pip install command. 3. The preceding command will install the Selenium Python module, and the environment will now be ready to accept Selenium commands to automate the browser.<\/p>\n Creating Python project in Eclipse<\/strong><\/p>\n We will now perform the steps to create a Python project in Eclipse and write down our first script to open the browser with the URL of our demo site application and close it. We will perform it for all three browsers: Internet Explorer, Firefox, and Chrome. <\/p>\n 2. Provide a name to the project, select Python interpreter, it is advisable to choose Quick Auto Config. Refer to the following screenshot:<\/p>\n <\/p>\n 3. Click on Next, and then Finish. Eclipse may ask you to open the Python perspective, select Yes.<\/p>\n We are now ready to write down scripts in Python to automate the browsers.<\/p>\n Automating Chrome browser<\/strong><\/p>\n To automate the Chrome browser, we first need to download the Chrome driver. Depending on the Chrome browser version we have on our system, and the operating system we are using, we need to download the suitable driver. It is advisable to ensure that you have the latest version of the browser. <\/p>\n 3. Here, we are taking an example of the Win32 system. So we download the Win32 ChromedriverZIP folder and extract the. exe file from it. Writing the first script<\/strong><\/p>\n We will now write our very first script to automate the Chrome browser, where we will launch the browser, open the application, and then close the browser.<\/p>\n <\/p>\n Let us understand the above script. The first statement from the Selenium import web driver will import Selenium classes in the file. This will allow usage of the Selenium commands in the script. So basically, we can create an object of WebDriver, and call different methods associated with it to handle the browser. It will fail if the Selenium module is not imported. The next statement initializes the browser object by creating an instance of the Chrome object and passing the path of the executable driver. With the Selenium WebDriver, every browser requires its own WebDriver to automate them.<\/p>\n The get method allows the browser to be launched with the URL that has been provided, and the quit method closes all the instances of the browser opened from this script.<\/p>\n Automating Firefox browser<\/strong><\/p>\n To automate the Firefox browser, we need to download the gecko driver. The geckodriver is the API which allows us to automate the Firefox browser. It acts as a bridge between Selenium and the browser. Flere as well, we need to select the driver version based on the operating system and the Firefox version. To download the driver, we need to visit the link: https:\/\/github.com\/mozilla\/ geckodriver\/releases<\/p>\n Select the driver according to your system; here we select for win64 from the following list:<\/p>\n Refer to the following script:<\/strong><\/p>\n from selenium import web-driver<\/p>\n Once we download the geckodriver for win64, we extract the geckodriver. exe file from the zip folder and put it in the driver’s folder in the Python project of Eclipse. Now we are ready to create the script which will open the application on the Firefox browser and close it:<\/p>\n <\/p>\n Automating Internet Explorer<\/strong><\/p>\n To automate the Internet Explorer browser, we have to download the IEdriverserver. exe file. This file is available on the https:\/\/www. seIeniumhq.org\/download\/ website.<\/p>\n Depending upon the operating system we are using, we will download that particular IE driver. Once the. exe file is downloaded, we will save it in the driver’s folder of our project, and write the following script to open the application in the IE browser:<\/p>\n The Internet Explorer Driver Server<\/strong><\/p>\n Conclusion<\/strong><\/p>\n So, in this chapter, we have learned how to set up our system, create projects and automate the three browsers. The official documentation of Selenium Python bindings is available here: https:\/\/seleniumhq. github.io\/selenium\/docs\/api\/py\/index.html Related Articles:<\/strong><\/p>\n Selenium Python – Installation and Setup In this chapter, we will learn how to prepare our system with the required software so that we are able to write our test automation scripts and execute them. We will note to have Python, Java Runtime Engine (JRE), Eclipse IDE, PyDev. After setting up this software for creating …<\/p>\n\n
\n
\n[download the latest version] [3.7]<\/li>\n\n
\n1. Open the URL: https:\/\/pypi.org\/project\/selenium\/
\nThe above URL provides information about the Selenium module of Python, the command to install it, and the documentation available with it.
\n2. To install the module, open the Command Prompt and type pip install selenium, shown as follows:
\nSSI Command Prompt<\/p>\nMicrosoft Windows [version 10.0.17134.706]\r\n(c) 2018 Microsoft corporation. All rights reserved.\r\n\r\nC:\\users\\write>pip install selenium<\/pre>\n
\nLet’s see in a step-by-step manner:
\n1. Go to File | Create New Project. In that window, select PyDev project. Refer to the following screenshot:<\/p>\n
\n1. To download Chrome driver, visit the link:
\nhttp:\/\/chromedriver.chromium.org\/downloads
\n2. Drivers are available as per the operating system type for download:<\/p>\n
\n4. In Eclipse, where we create our project, we create a folder there, called drivers. We copy this chrome driver. exe file into that folder. So finally our project would look as follows:
\nI MG<\/p>\nbrowser =webdriver.Chrome (executable_path=r \\Ectipse\\BPB\\seleniumpython\\Selenim&thPython\\drivers\\chromedriver.exe}\r\nbrowser.get{'http:\/\/practice. bpbonline,com\/catolog\/index.php'}\r\nbrowser.quit()<\/pre>\n
This is required if you want to make use of the latest and greatest features of the WebDriver IntemetExplorerDriver, Please make sure that this is available on your $PATH (or %PATH% on Windows} in order for the IE Driver to work as expected.\r\n\r\nDownload version 3.14.0 for (recommended) 32 bit Windows IE<\/span> or 64 bit Windows IE CHANGELOG<\/span><\/pre>\n
\nNow our system is ready for us to write down our Selenium tests for the multi-browser support available with Python programming languages, aiding the manual testers by automation and reducing the workload. In our next chapter, we will learn how to work with different types of WebElements, understand the concept of synchronization, and parameterization, which forms the basic building blocks for automation.<\/p>\n\n