You are on page 1of 52
91321, 708 PI Selenium WebDriver wth Python Tutorial -javatpoint Selenium with Python Tutorial In this tutorial, we will learn how to run a Selenium test script using Python Programming language. Before going further in this tutorial, first, we will understand some essential points which will help us to implement test scripts in Python. © What is Selenium? © Why we use Selenium for automation testing? © What is Python? © Why's Python useful for automation testing? © Selenium with Python © Configure Selenium using Python © Configure PyDev in Eclipse What is Selenium? Selenium is an open-source testing tool, which means it can be downloaded from the internet without spending anything. Selenium is a functional testing tool and also compatible with non- functional testing tools as well For further information about Selenium, refer the below link: https:/Awww javatpoint.com/selenium- tutorial hitpsshwwujavatpoint comiselenium-python ais 9113721, 7.08 Pa Selenium WebDriver wth Python Tutorial -javatpoint Why we use Selenium for automation testing? Selenium is one of the most popular automation testing tools. Here automation testing is a process of converting any manual test case into the test scripts using automation tools such as Selenium. Following are some aspects that, why we prefer Selenium for automation testing: © The Selenium test scripts can be written in any programming languages like Java, Python, C#, Ruby, Perl, as so on. © The Selenium test scripts can be executed in many operating system platforms such as Windows, Mac, Linux, etc. © And the test scripts can be implemented on different browsers like Google Chrome, Mozilla efox, Internet Explorer, Safari, etc. ©. The Selenium testing tool can be associated with other testing tools such as TestNG, Junit for managing the test cases, and generating test reports. © To achieve continuous testing, Selenium is integrated with Maven, Jenkins, and Docker testing tools. What is Python? © Python supports the Object-Oriented Programming approach to establish the applications. It is simple and easy to learn and provides lots of high-level data structures. It is an open-source language. © Itisa high-level and interpreter scripting programing language. htpshwwjavatpoint comiselenium-python ais 9113721, 7.08 Pa Selenium WebDriver wth Python Tutorial -javatpoint © Python makes the development and debugging fastbecause there is no compilation step included in Python development, For further details about Python language, refer the below link: https://www javatpoint.com/Python- tutorial Why is Python useful for automation testing? © Python is very useful for automation testing because it supports multiple programming patterns. © Python has many built-in testing frameworks such as Pytest and Robot, which covers the debugging and faster workflow. © It is an interpreted language means the interpreter implements the code line by line at a time that's makes debugging easy. © Python is Cross-platform Language; that's why it can run on different platforms like Windows, Linux, UNIX, and Macintosh, © Python can be easily implemented with other programming languages such as C, C++, JAVA, etc, Selenium with Python There are two ways to run a Selenium test scripts with the help of Python: 1. Configure Selenium using Python 2. Configure PyDev in Eclipse. Let us see how we configure Selenium with the help of Python programing language: Configure Selenium using Python There are following steps to configure Selenium using Python © Download and install Python on Windows ©. Install Selenium libraries in Python © Download and install PyCharm © Create a new project and write the Sele © Run and validate the test scripts. hitpsshwwujavatpoint comiselenium-python ais 913721, 708 PM Selenium WebDriver with Python Tutorial - javatpoint Download and install Python for Windows In this section, we will see how we download and install the Python for Windows platform. Download the Python To download the latest version of Python for Windows Platforms, refer the below link: https://mmw.Python.org/downloads/ © Once we clicked on the above link, the latest Release version list is shown, where we clicked on the Python 3.8.1 version as we can see in the below screenshot: Release version pytnon 3.7.6 Python 3.6.10 Python 3.5.9 Python 3.8.8 Python 2.7.17 Python 3.7.5 € 2 C @ pythonorg/aownloads/ Releace date Dec. 18,2019 ec. 18,2018 Dec. 18,2019 Nov. 2,2019 Oct. 29,2019 ot 19, 2019 Oct. 15,2019 Looking for Python 2.77 See below for specific releases Looking for a specific release? Python releases by version number: ee & & & & & Download b Downloed Download Download powntaaa Downloed liek for more Releate Notes Releace Notes Release Notes Release Notes Roleace Notes Releate Notes Release Notes hitpsshwwujavatpoint comiselenium-python 9s7 an3i21, 7.08 Pe Selenium WebDrver wth Python Tutorial -jovatpoint ©. The Python-3.8.1 version window will appear on the screen, then scroll the page little-bit and find the File section, and the click on the Windows x86-64 web-based installer link for the Windows operating system as we can see in the below screenshot: fe > CG fi pythonorg/downloads/release/aython-381/ & Version Operating Description MDS Sum FileSize GPG System tarball Source f2ista2tssa7ade7a9c1Te7ecssbabed 23978360 SIG release XZ compressed source Source batbastda7scobt9soce2sefB0cacbS7 17828408 SiG tarball release macOS 64-bit installer MacOSX for OSX10.9and _d1bos6sS312b|bIf4etLbosd6a451023 28051411 SIG later Windows help fle Windows febbieaccs6fidesethfettersea6ct2 «8480893 SIG Windows x86-64 Windows Fo adostasrazissdasnebb5c522b211061 8013540 SiG embeddable zip file AMDSS/EMG#T/A64 Windows x86-64 Windows for 3escs2fsHtofedbeGas28e912b7afdb1 sic executable installer AMD54/EM64T/x64 Windowsx86-64web, Windows for (662961733¢c947839a73302789016145 SiG based installer AMDSS/EMGATINGS Install the Python After downloading the Python for Windows-64 bit, we will be ready to install the Python, To install the Python, follow the below process: © Once we double-click on the downloaded executable file, the Python 3.8.1(64-bit) setup window will appear on the screen, where we have two options available to install the Python, which are: © Install Now © Customize installation © We will click on the Customize n, and select Add Python 3.8 to path checkbox as, we can see in the below image: htpshwwjavatpoint comiselenium-python esr 9199, 708 PM Selenium WebDrver wih Python Tutor -jvaipaint Python 33.1 GtEid Setup = x Install Python 3.8.1 (64-bit) Select Install Now to install Python with default settings, or choose Customize to enable or disable features. |> Install Now (CAUsers\sTP\AppData\ Local Programe\Python\Python38 Includes IDLE, pip and documentation Creates chorteuts and file sesaciatione —> Customize installation Choose location anc features python istalaunener for al uses veconeende windows Canal nal Features will appear on the screen, © After, click on the customize installation, the O] where we can select and deselect the features according to our requirements. © Then, click on the Next button, to proceed further as we can see in the below image: Pyton 33.1 (Eid Setup = x Optional Features Documentation Installs the Pyinon documentation fle Bip Instals pip. which can download and install other Python packages MA telfte and IDLE Instals tkinter and the IDLE development environment. Python test suite Instals the standard library test suite, py launcher I users (requires elevation] Use Programs and Features to remove the 'py' launcher. python wind Ws | Back Cancal hitpsshwwujavatpoint comiselenium-python is? 9113721, 7.08 Pa Selenium WebDriver wth Python Tutorial -javatpoint ns available, where we © Once, we clicked on the Next button; we have a list of Advanced Op' can select the options based on our needs and also make sure that the Install for all users is selected. © We can also customize the install location according to our convenience by clicking on the Browse © After that, click on the Install button, to install the Python as we can see in the below screenshot: Pyton33.1 tit Setup = x Advanced Options HZ install for al users «jam ‘Associate fles with Python (requires the py launcher) Create shortcuts for installed applications [Add Python to environment variables C1 Downicad debugging symbo's C1 Download debug binaries (requires VS 2015 or later) Customize instal location C:\Program Files\Python3@| Browse python f windows Back © The installing process is getting started after clicking on the Install button as we can see in the below screenshot: hitpsshwwujavatpoint comiselenium-python ais 199,708 PM Selenium WebDrver wih Python Tutor -jvaipoint Pyton 33.1 Gti Setup = x Setup Progress Installing: par aan iv se >—_ = python windows Ga © When the installation is done, we got the confirmation message as Setup was successful, which means that the Python is stalled successfully for the Windows operating system. © Then, click on the Close button, to close the setup window as we can observe in the below screenshot: D Python 381 GbiD Setup = x Setup was successful << Special thanks to Mark Hammond, without whose years of freely shared Windows expertise, Python for Windows would stil be Python for DOS. New to Python? Start with the online tutorial and documentation, See what's new in this release. ® Disable path length limit Changes your machine configuration t allow programs, including Python, to bypass the 260 character MAX, PATH" limitation python, windows hitpsshwwujavatpoint comiselenium-python ois 9113721, 7.08 Pa Selenium WebDriver wth Python Tutorial -javatpoint After that, we will check whether Python is installed successfully and working fine or not. So for this, we will open our command prompt, and type the command as Python and press the Enter key, and it will open the Python interpreter shell where we can implement the Python program as we can see in the below image Bad CAWINDOWS \ystemnsAcmd exe - python - ao x Installing the Selenium libraries in Python Once we successfully install the Python in our operation system, we will install the Selenium libraries For this, we will execute the following command in our command prompt Python -m pip install -U Selenium And, this command will successfully install the latest Selenium package ie, Selenium -3.141.0 added to the libraries as we can see in the below image: hips javatpoin.cor 10157 9113721, 708 Pa Selenium WebDriver with Python Tutorial - javatpoint BY CAWINDOWS\system32\cmdece After that executing the above command, it will create the Selenium folder automatically having all the Selenium libraries as we can see in the below screenshot: 11) BO = sitepackages - a x Home share View e = FFiseean = ne L 4 see none Pinto uck Copy Paste jo BRenme New Properties ede SE @ eBlimen seecion cipboars Organize New open select € » A (lb LocaiDisk(C) > Uses > STP > Bython > Lib > site-packages vO | Searchsit.. f B Downloads #% Name * Date modified es ih aesae p20557 ile folder Pictures Bo Fle folder ecitimagetsten|)) [ll pip-1923cistinfo ie selenium new to selniur python seleniumm-3.141.0.45 a a a a talend tutorials 7 eetuptoole ‘older © OneDiive 1 setuptools-41.2.0.ditinfo older meee i uetiba ile folder 5 1) urtib3-1.25.7.st-info File folder Senate B easy insta 9 thon File TB Desktop =) README 2 Tect Document Download and install PyCharm Once we successfully install the Selenium libraries into Python, we are ready to download Python IDE that is PyCharm, ‘To download the PyCharm, follow the below process: © Refer the below link, to download the PyCharm https://www.jetbrains.com/pycharm/download/#sectio! indows hitpsswojavatpoin.cor 1187 9113721, 7.08 Pa Selenium WebDriver wth Python Tutorial -javatpoint © Once we clicked on the above link, we will get the below window, where will click on the Download button under the Community section for the Windows € > GB jetbrains com/pycharm/download/#aaction=windows * Download PyCharm win Swe] Mac Linux Professional Community Web For pure Python di ment. With System requirements Free trial Free, open-source installation Instructions ment © After that, double-click on the executable file to install the PyCharm, and the PyCharm Community Edition Setup window will appear on the screen, where we click on the Next button to proceed further as we can see in the below image: htpshwwjavatpoint comiselenium-python vais7 91321, 708 PM Selenium WebDriver with Python Tutorial -javatpoint BB Pycharm Community Eston Setup = x PC Welcome to PyCharm Community Edition Setup Setup wil uide you throuch the instalaton of PyCharm (Conmurity Eton. Itigrecommended that you cose al other apolications before starting Setup. This will make it pessble o undate Felevanteysten flee withouthaving to reboot your computer. ick Next to contnue. © In the next step, we can Choose Install location by clicking on the Browser button, then click ‘on the Next button for further process. Bil Pycharm Community Edition Setup - x Choose Install Location qj (Choose the fldern which to nstal PyCharm Community Edtion ‘Setup il installPyCharm Conmurity Eaton n the following folder. To instal ina ferent Felder, ick Browse and select ancther fader. Cick Next 9 contre. Destination Felder (Ca\sers JIPPython PyCharm Connunity Eden 2019.3. Space required: 657,58 Space avaiable: 6.2 68 © In the next step, we have some Installation Options available, and we can select them based on our requirements. © After that, click on the Next button as we can see in the below image: hitpsshwwujavatpoint comiselenium-python 13187 9113721, 708 Pat Selenium WebDriver wth Python Tutorial -javatpoint PyCharm Community Ection Setup = x Inetallation Options: CConfure your PyCharn Community Editon instalation Create Desktop Shortest Upcate PATH varable (restart needed) Add launchers dir to the PAT Update context mers (D)Ade "open Folder as Project™ Create Acsocatone Oey fk ene © Then, click on the Install button to install the PyCharm, as we can see in the below screenshot: PyChatm Community Ection Setup = x Choote Start Menu Folder Choose Start Menu folder for the PyCharm Community Eton shoraits, Select the Start Maru flde in which you would ke to create the pregran's shortuts, You Canalo entre name tocreete anew folder 7 * ecesscries | DManager us ‘drinisrative Teale Edipoe ts Jaa Java Develooment at Maintenance Microsoft Office 2016 Tools Mysqu [Pye 3.8 Bade cancel © As we can see in the below image, the installation process is getting started htpshwwjavatpoint comiselenium-python sais7 913721, 708 PM Selenium WebDriver with Python Tutorial - javatpoint BB Py charm Community Esition Setup = © Then, click on the Irstaling Please wait while PyCharm Conmunity Eton s beng installed rit. 12 jer... 100% roxy-vole-1.0.53,2ar., 100% conguration-storempl Jer... 100% Javan. actvation-1.2.0.3".. 1002 rd-sving-0, 193.2010... 100% batkeece-1.12.0:8,jar.. 100% 1305... 100% Javan ennolation-ep1.3.2jar.. 190% Foup-1 12. Ljar.. 100% FESOUTCES eT... 65% > ° - Sinput class—"ghtyt gsfi" mentength“2000" fee] =~ type="text" jsaction="saste:puy28a" aria-sutocomplet "poth” aria-haspopup-"folse” autocapitalize-"cf®™ autocomplete="off" autacorrect="o#f” role="conbobox” spellcheck-"false" titie-"Search” value aria-label- > Here the sample code driver-find_element_by_name("q").send_keys("javatpoint') Step6 ‘Once we identify the Google search text box, and we will identify the Google Search button. So for this, follow the below process: ©. Right-click on the Google searchbutton, and select the Inspect option from the given pop-up menu as we can see in the below image: AleeLaft Arrow AitsRight Arco CelR cules culep ‘ransate to English © AdBiock — best ad blocker > -—_ Viewpage source Cte Google Search CtrleShiftel hitpsshwwujavatpoint comiselenium-python 2287 91321, 708 PI Selenium WebDriver wth Python Tutorial -javatpoint © The developer tool window will be launched with having all the specific codes used in the development of the Google search button. © Then, copy the value of its name attribute that is "btnK" as we can see in the below image: Google inputgNO89b 127.4135 I'm Feeling Lucky Networe > e202 x “Googie Search” aria-Labe! “covgle Searchi[nam=—"HEnT"] type="submit” dete-ved= ~QanukEscustiinPvnaniGyS0mFcPeDL00SdUDCko"> ~~ $0 ing Lucky" eria- linge” velue-"T'm Fe And, the sample code is as following: driver-find_element_by_name("btnk").send_keys(Keys.ENTER) Step7 In the last step, we are closing the browser. And, the sample code for closing the browser is as follows: driver.closeQ) Our final test script will look like this, after completing all the above steps from Selenium import webdriver import time htps:hwwwjavatpoint comiselenium-python 27 911921, 708 PM Selenium WebDrver wth Python Tutorial -jovatpoint from Selenium.webdriver.common.keys import Keys print(‘sample test case started") driver = webdriver.Chrome() #driver=webdriver firefox() #driver=webdriver.ie) #maximize the window size driver.maximize_window() #navigate to the url driver.get("https://www.google.com/") #identify the Google search text box and enter the value driver find_element_by_name("q").send_keys("javatpoint") timesteep(3) #click on the Google search button driver find_element_by_name("btnk").send_keys(Keys. ENTER) timesteep(3) #close the browser driver.close) print(‘sample test case successfully completed") Note: Import time: Time is a Python module, which is used to handle the time-related tasks such as time sleep() from Selenium.webdriver.common.keys import Keys: Here, we are adding Keys libraries from Selenium, like in the above code, we are using the Enter key instead of elick() method to perform a particular scenario. Run and validate the test scripts Once we are done with writing the Selenium test script, we will run our test scripts. Here we will run our test scripts in two ways © Run in Python IDE © Run in Command Prompt htpshwwjavatpoint comiselenium-python 2057 9113721, 7.08 Pa Selenium WebDriver wth Python Tutorial -javatpoint Run in Python IDE So, for this first, we will see how to run the Selenium test script in Python IDE © Right-click on the code, and select Run ‘Samplet’ from the popup menu as we can see in the below screenshot ee © When we run this script it will give an exception because we don't have the Chrome driver executable file as we can in the below image: To overcome this exception, we will download the chrome driver executable from below link: https://chromedriver.storage.googleapis.com/indexhtmI?path=79.0,3945,36/ © Once we click on the above link, we will click on the zip file based upon our operating system platform. Like we have Windows platform that's why we clicked on thezip to download the Executable file as we can see in the below screenshot: hitpsshwwujavatpoint comiselenium-python 25157 o11921,708 PM Saleium Web rv vit yon Taare Index of /79.0.3945.36/ Name Last modified Size ETag @ Parent Directory - ® FR) chromedriver_linux64 zip 18.20.03 4.65MB 77ecb31470c63c2dfs602622000f916| chromedriver_mac64.zip 18:20:05 6.59MB s7dzavezsz0sz0sdc2d7seteesdesd1s| 18:20:06 4.07MB 966Sbe96d739035efdfo1es4tanetact| 18:20:10 O.00MR cdebdsdssbhessse7thofoscfeaa7sb| i 8 8 2019-11-11 ° After downloading the exe file, we can paste this file to the Python folder and unzip it ° Then, we will create one more folder called libraries as Browsers in the Python IDE. ° Right-click on the Project(SeleniumTest) - New ~ Directory as we can see in the below screenshot: © And, we will add all the driver's executable files in the Browsers folder manually. ° For this, we will copy the chrome driver exe file from the Python folder, and paste in the Browser folder as we can see in the below image (]> Percy tocaibexi@y > Uren) TIP > PychamnProjcts > Sekriumiet 7 Browsare Name Date modified Type Size Tel geckodiver 019 124M Application 8,508 KE PM Application 3,100KE © Now go to PyCharm IDE, and copy the Absolute path of chromdriver.exe file as we can see in the below screenshot: pene © Replace the statement “driver = webdriver.Chrome()" with a statement given below: driver=webdriver.Chrome(r"C:\Users\JTP\PycharmProjects\SeleniumTest\Browsers\chromedriver.exe" hitpsshwwujavatpoint comiselenium-python 2657 9113721, 7.08 Pat Selenium WebDriver wth Python Tutorial -javatpoint Note: Here, we will use “r* to overcome the Unicode error. As we can see in the below screenshot, if we do not put r in the code, it will generate the Syntax Error. © After that, we will run the sample1 once again, and it will execute the code successfully as we can see in the below image: The above test script will launch the Google Chrome browser and automate all the test scenarios htpshwwjavatpoint comiselenium-python 20ST 91321, 708 PM Selenium WebDriver with Python Tutorial -javatpoint 7D jaatpaint € x josh LUCAAWaq=javatpointeoq=javatpointlags I=psy-a Q Al Books —@ Shopping News E] hmages t More Settings Tools Apout 110,000 resus (050 seconds Javatpoint: Tutorials List httos:/(www javatpoint.com + Tutorials, Fee Onin Tutorials, Javatpoint provides tutorials nd interview questions ofall technology like java torial, andro, java frameworks, javascript, jax Java C++ ‘Java Tutorial or Leam Java or Core Leam G++ tutorial or beginners “ava Tuono Java and professionals wt Python Android Studio Tutorial sr yon Tora! or begets Lem Ano otal or besos protect vith ca pretest nc Learn C Programming Learn HTML Tutorial eam C Tuotia er € Programing Leam HTVAL Tutorial or HTL 5 More results rom javatpoint.com > Run in Command Prompt To run the above test script in the Command prompt, follow the below process: © Copy the location of the Sample1.py file as we can see in the below image: hitpsshwwujavatpoint comiselenium-python 227 an3i21, 7.08 Pe Selenium WebDrver th Python Tutorial -jvatpont © And paste in the command Prompt, first go to the particular folder then enter the below command: Python Sample1.py © Then, press the Enter key as we can see in the below screenshot that the sample test case stared. © And after automating all the scenarios, it will show the message as a sample test case successfully completed. HH CAWINDOWS\zystem22\ cmd.exe Configure PyDev in Eclipse This is another way to execute the Selenium test script using Python. In this section, we will install PyDev in Eclipse, then implement the Selenium test scripts on it © Install PyDev © Execute the Selenium test scripts using Python. Install PyWDev To install the PyDev in Eclipse, follow the below process: ° PyDev installation ° Set the preference © Create a new project ° Create a new package ° Create a PyDev Module hitpsshwwujavatpoint comiselenium-python 20157 91321, 708 PI Selenium WebDriver wth Python Tutorial -javatpoint PyDev Installation Before start installing PyDev in Eclipse, make sure that we already installed the latest version of Eclipse IDE. © Double-click on the Eclipse IDE software. © Then, go to Help in the toolbar and select Eclipse Marketplace option in the given pop-up menu as we can see in the below screenshot S ecipse-workspace - Eclipse IDE seech, Pejet fun Window [ROB] wwekone ee Help Contents Search ‘Eclipse User Storage Perioim Setup Tasks.» Check for Updates Install New Software. Hee €@ < S About EclipselDE ® Contribute © Eclipse Marketplace window the Enter ‘open, and in the search area, we will type PyDev and press © Then, click on the Install button as we can see in the below screenshot: ‘© Eclipse Marketplace a x Eclipse Marketplace Select solutions to install, Press Install Now to proceed with instalation. Press the "more info" linkto leain more about a zolution. Search] Recent| Popular| Favorites| Installed | () Giving loT an Edge| PyDev - Python IDE for Eclipse 7.5.0 PyDev is a plugin that enables Eclipse to be used as a Python IDE (supporting also Hython and IronPython). @PyDey wer advanced tye inference techniques which sllow.. more inf by Brainwy Software EPL ‘All Mavcet= | [All Categariex v) (Go IDE Python Aptana Pydev Django ... iw] [nv 130m (1327 st men htpshwwjavatpoint comiselenium-python 2057 9113721, 7.08 Pa Selenium WebDriver wth Python Tutorial -javatpoint © Once we clicked on the install button; it will start the installation process and then click on the Confirm Button for further process as we can see in the below process: © Eclipse Marketplace o x Confirm Selected Features >» Press Confirm to continue with the installation. Or go back to choose more solutions to install ey [A@s PyDev - Python IDE for Eclipse 75.0 tip pydev.org/update PyDev for Eclipse (required Dydev Mylyn Integration << install More || Confirm > Finish Cancel © Then, select | accept the terms of the license agreements option and click on the Finish button to finish the installation process as we can observe in the below image: hitpsshwwujavatpoint comiselenium-python 3187 Selenium WebDriver with Python Tutorial -javatpoint ans, 7.08 Pet © Ecipse Marketplace Review Licenses > Licenses must be reviewed and accented before the software can be installed ey License text: Eclipse Public License Licenses: » Eclipse Public License Eclipse Public License -v 1.0, @ leccept the terms of the Heense agreements ‘Odo not seceptthe terms of the icence agreements 4 Finish Cancel « acme Saves > Pepe henaeon > eee © The Preference Window will open where we will expand the PyDev then again expand the Interpreters and select the Python Interpreter from the given list. Click on the Browse for Python/pypy.exe option as we can observe in the below image: PyDev ~ Interpreters — Python Interpreter S Pieteences ‘ype fitertot Python Interpreters Gener Ant Grade Nome Location Help Ineal/Upaate ew with Pipene eee Python interpreters (9: pythoncere pypy.exe). Double-iceto rename fer pitvon/pyy Oe) even Config fist in PATH re aaa ous each ima erence Le ay ue © Once we click on the Browse for Python/pypy.exe, we will provide the Interpreter Name and Executable file path of Python. © For this, in the Select interpreter pop-up window, click on the Browse button, and browse the Executable file path of Python in our local system, and click on the OK button as we can see in the below Image htpshwwjavatpoint comiselenium-python sais7 9113721, 708 Pa Selenium WebDriver ith Python Tutorial -javatpoint © select interpreter x Enter the nameand executable of your interpreter Interpreter Name: pythor! iepicteEneutable [Cities PR oipytionse id =»> m% Grea © After that, click on the Select All button, then click on OK button to select the folders as we can see in the below screenshot: © Selection needed o x Select the folder: to be added to the SYSTEM pythonpath! IMPORTANT: The folders for your PROJECTS should NOT be added here, but in your project configuration. (Checlehttp://pydev.org/manual_101_ntespretes:html for more details ZB AtlersPPythor\DLe Zw CAUsers TP Python BvvsesuTPAPython ZB CAUsere TP ippData\Roaming\Pythor\PythonZD\site-packages © Si CAusers MP Python ibis: packages Select All natin Workspace ap « Gal Deselect All © Then, click on the Apply and Close button, to complete the setting preference process as we can see in the below screenshot: hitpsshwwujavatpoint comiselenium-python s4is7 9113721, 708 Pa Selenium WebDriver with Python Tutorial -javatpoint Python Interpreters @vorey >ython interpreters (eg: pythonvere pypy.exe). Double-click to rename, “ Name Location hon prey ess © pythen CAUsers\TP Pythen\python exe | ‘New wath Piper Config fis in PATH Choose rom lit Remove Up ‘8 Packages Bi Libraries Forced Buitins Predefined MG Environment @ String Substitution Variables Lora (pip [7 founa) Version oe Tisceback (most recent call lst Fle "c\urereyto\python\lib\vunpy py", ine 103, in een ‘eturn_sun_code(code, main. globas, Hone, — Fle"c\usets\to\python\Iib\vunpy py", ine 86 in + exec(code, un glebals) Fle" CA\Ucer2 JIP\Pythen\Serigts\pip.exe\_main_. Typeton ‘module abject ie net callable Load conds ery vars before run Resor Defaults! | ool |Y Cancel © After setting the preference successfully, we will create a new project in Python, Create a new project To create a new project, follow the below steps: © Go to the File menu then go to New and select Other option in the popup menu as we can see in the below image: + New ~ Other hitpsshwwujavatpoint comiselenium-python 38157 9113721, 708 PA Selenium WebDrver with Python Tutor jvatpont [S eclipse-workspace Eclipse IDE Edt Source Refactor Novigate Scorch Project Run Window Help Beste ‘Open Fi. (pen Projects from File System. Recent Files Java Project Project. Package Class Close Cate lose Al Ceteshireew Intefece Enum ane Annotation Save Ae Source Folder Save All Chile Shifes Revert Java Working Set Folger BSEBeegek IS File Untitled Text File Task JUnit Test Case ize Move Rename Refresh Convert Line Delimitere To Example Prin. © Once the Other option is selected, [New] window will open where we will expand PyDev and choose PyDev Project, and click on the Next Button as we can see in the below image: S New a x Select a wizard =o I Lat) Warde [fypeFiker to os : Gh yew djange Project 43, PyDev Google App Engine Project B) PyDev Module eee (& Source Folder safe 5B is ie E @ @ Add project directory to the PTHONPATH O Greate’stefolderand add itto the PYTHONPATH (O Greate links to existing sources (select them on the next page) (Don't configure PYTHONPATH (to be done manually later on) Working sets Tada project to working sets New. ® Cancel © The Python project is created in the Eclipse as we can see in the below image: hitpsshwwujavatpoint comiselenium-python amis 913721, 708 PM eclipse workspace ~ Eclipse IDE Fre Edit Navigate Search Project Pydev Selenium WebDriver with Python Tutorial - javatpoint iv il igi e+ Or aria 4 If PyDev Package Explorer 23 Bs python (C\Users.Python\python.cxe) Bi Ecternal Libs > BA Forced buitins 2 TP /PyAhon BA Predefined Completions © When we are done with creating a Python project, we will create a new Python Package. Create a New package Follow the below process, to create a new package: © Right-click on the newly created project(SeleniumTest), go to New, then select PyDev Package in the given Popup menu as we can see in the below image: 5 eclipse-workspace Eclipse IDE File Edit Nevigete Scorch Project Pydev Run Window Help BG Bit O-Q-iB Pid HrDaray HB PyDer Package Explorer 29 oe vi Selenium BAEt — Golnto 9 File > RAF coy Folder >eur ee . ore nto tise ee leis = (Source Folder Remove from Context Ctrls Ake ShiftsDown [A Example. © Then provide the name of the PyDev Package, and click on the Finish Button as we can see in the below image: hitpsshwwujavatpoint comiselenium-python 2057 9113721, 708 Pa Selenium WebDriver with Python Tutorial - javatpoint e o x Create a new Python package Source Folder | /SeleniuivTest Browse. Name 8 > Lae Ga © Aswe can see in the below screenshot that the new package is created. [2 PyDev Package Explorer =o BS|e> SeleniurvTest [i Demo] TE _int_py @ python (C:\Uses.. Python\python.ece) i Bctemal Libs 28) Forced builtins 2 MP/Python Bl Predefined Completions Bi System Libs After creating a PyDev Package, we will create a PyDev module. Create a PyDev Module To create a PyDev module, follow the below process: ©. Right-click on the newly created Package[Demo], then go to New and select PyDev Module from the given list as we can see in the below screenshot: Demo — New ~ PyDev Module htpshwwjavatpoint comiselenium-python 9187 91321, 708 PM Selenium WebDriver wth Python Tutorial -javatpoint HH PyDev Package Explorer 24 = Beno aS eer]? © Uf Seleriumiect Delete * 4B PyDev Package Move. oon OF ble Tarpon fon Conia Chi-AlteShit-Down | £9 Othe. chen © Then give the PyDev Module name [Test], and click on the Finish button as we can observe in the below screenshot: e o x Create a new Python module ‘Source Folder| /SeleniumTest Package [Demo m=] fC] ) a> Ce fone | © After that, we will select the Empty template from the given list, and click on the OK button as we can see in the below image: htpshwwjavatpoint comiselenium-python 40157 91321, 708 PM Selenium WebDriver with Python Tutorial - javatpoint e o x Template j<émpiy> | ‘Module: CLI (argperse) Module: CLI (optparse) Module: Clare Module: Main. ‘Module: Unictest ‘Module: Unittest with setUp end teerDewn ® mo] | Gn © Once we have done with creating a Python model, we are ready to write and execute our Selenium test scripts. Write and Execute the Selenium test script Write the Selenium test Script For our testing purpose, we will perform a Login test on the Gmail application, In this test, we will automate the below test scenarios: Steps Actions Input Expected Result 1. Import web driver from Web driver should be imported. Selenium, 2. Open the — Google The Google Chrome browser should Chrome browser. be opened, hitpsshwwujavatpoint comselenium-python 487 913721, 708 PM 3. Maximize the browser and delete all the cookies 4 Navigate to the home page Gmail application. 5. Identify the username text box and pass the value. 6 Click on the Next button. . Identify the password text box and pass the value. . Click on the Next button. 9. Close the Browser. Follow the below ste} Step1 In the first step, we will import the web driver with the help of the following stateme from selenium import webdriver Step2 Selenium WebDriver with Python Tutorial -javatpoint https://www.gmail.com xyz11@gmailcom seb After that, we will open the Google Chrome browser. The browser should be maximized, and cookies should be deleted. The Gmail home page must be displayed. The username text box should be identified should be entered in the username text box. and value The next button should be clicked. The password text box should be identified should be entered in the Password text box. and value The next button should be clicked. ‘The Browser should be closed. © To launch the Google Chrome or any other browser (Firefox, ie, etc), we need to download the executable files of the Chrome driver from the given link https://sites.google.com/a/chromium.org/chromedriver/downloads © Then, paste the downloading exe file in the Python package (demo) to overcome the ‘exceptions as we can see in the below screenshot: hitpsshwwujavatpoint comiselenium-python 4287 91132, 708 PM Selenium WebDrver wth Python Tutorial -jovatpont | « Uses > JP > eclpie-workspace > SeleniumTest > Demo vlO) | Seare Neme . Date modified Tye PY File TE chromedtiver [El geckodeiver, Bet The sample code is as below: #open Google Chrome browser driver = webdriver.chrome() Step3 In the next step, we maximize and delete all the cookies of the browser window. Here the sample code: #maximize the window size driver.maximize_window() #delete the cookies driver.delete_all_cookies() Step4 In this step, we will navigate to the Gmail application URL. The sample code is as below: #navigate to the url driver.get("https://www.gmail.com") Step5 Once we navigate to the URL of the Gmail application, we will identify the username text box and passing the value of it hitpsshwwujavatpoint comiselenium-python 4257 9113721, 7.08 Pa ©. Right-click on the username text box. Selenium WebDriver wth Python Tutorial -javatpoint To identify the username text box, follow the below process: © And select the Inspect option in the given pop-up menu as we can see in the below screenshot: Google Sign in ‘to continue to Gmail Email or phone Email Win=Pericd Fe S uy ; Le Paste cule Paste as plain text CteleShit-ev é 3 +A Spel check Writing Direction © Aleck — best blocker Inspect privately. ig Privacy Terms © The developer tool window will open with all the specific codes used in the development of the usernametext box. © Then, copy the value of its id attribute that is: identifierld as we can see in the below image: htpshwwjavatpoint comiselenium-python ast

You might also like