How To Upload Files Using Selenium Webdriver Python
Selenium WebDriver is 1 of the about popular tools for Spider web UI Automation. And no better than Python can complement it to automate a broad range of spider web applications. Hence, we brought this Selenium Webdriver Python tutorial to ramp you upwardly quickly on the task. So allow'due south showtime by understanding what all we have to do in UI automation testing.
Web UI Automation means the automated execution of the actions performed in a web browser window like navigating to a website, filling forms that include dealing with text boxes, radio buttons and drop downs, submitting the forms, browsing through web pages, handling popular-ups and then on. Selenium WebDriver is the one that tin automate all these tasks. Information technology can collaborate with all types of Web browsers available till engagement like Firefox, Internet Explorer, Safari, and Chrome, etc.
Near of the time, nosotros use Java and Eclipse to create a Selenium Webdriver project. In this mail service, we'll utilize Python to replace Java and demonstrate how the Selenium Webdriver Python integration works to achieve Web UI automation.
Selenium is an open source, and its library is bachelor in dissimilar programming languages to perform the Web UI Automation testing, and Python is one of them.
Selenium WebDriver Customer Library for Python enables us to apply all the features available with Selenium WebDriver and collaborate with Selenium Standalone Server to perform Automatic testing (both remote and distributed testing) of browser-based applications.
It could be relevant for you to know nearly the team and its members who drove the development of the Selenium WebDriver Python libraries, they are Miki Tebeka, Adam Goucher, Eric Allenin, David Burns, Luke Semerau, Jason Huggins and MaikRoder.
The Selenium Webdriver library is compatible with a series of Python versions that includes Python 2.six, ii.7, and iii.2-iii.5.
In this post, nosotros'll mainly discuss the post-obit subjects and so that yous get acquainted with Selenium Webdriver Library for Python:
Table of Content
- Installation of Python and Selenium Webdriver Package for Python
- Selecting Python Editor
- Prepare Your First Selenium Webdriver Automation Script Using Python
- Write a Script for Firefox Browser
- Decoding the Automation Script
- Creating Selenium Webdriver and Python Scripts for
- Internet Explorer
- Google Chrome
- Unlike Navigation Methods in Python
- How to Switch betwixt Windows
- How to Switch between IFrames
- Learn to Handle Warning & Popup Boxes
- How to Locate Elements Using Selenium Python
- How to Utilise Selenium WebDriver Waits in Python
- Using Python UnitTest for Automation
- Summary
Let'south now learn every lilliputian detail about setting up the Selenium Webdriver Python environment and write our showtime Selenium test script in Python.
Selenium Webdriver Python Setup for Spider web Automation
Installing Python
On Linux Distributions, MAC OS 10, and Unix machines; Python is by default installed.
However, on Windows machines, information technology needs to be installed separately. Python installers for different Operating Systems are available at the following link:
➡ Download Python Installer
For this tutorial, nosotros've downloaded the latest version of Python (3.5.1) and used it for the examples given in the different sections of this tutorial. Yous tin can use the direct link given beneath to install the Python iii.5.one.
➡ Download Python (three.5.1)
Subsequently you lot run the Python installer, it also installs the <pip> tool which is Python'due south package manager. It facilitates the installation of advanced packages like the Selenium Webdriver.
Installing Selenium Webdriver Python Packet
There are 2 unique ways to prepare up Selenium Webdriver with Python.
A- Employ PIP package managing director to install Selenium with Python
1- Starting time become to the directory where you've installed Python.
For example, we accept the latest Python version 3.5.1, and its location is in the <C:\python\python35> folder.
2- Apply the <pip> tool to install the Selenium Webdriver parcel.
C:\python\python35>pip install selenium Collecting selenium Downloading selenium-ii.53.0.tar.gz (815kB) ←[K 100% |################################| 819kB 108kB/south ta 0:00:01 Installing collected packages: selenium Running setup.py install for selenium Successfully installed selenium-ii.53.0 ←[33mYou are using pip version 7.i.2, however, version eight.1.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
If yous want to upgrade the currently installed Selenium Webdriver packet then, just add the -U flag to the previous <pip> control. Since we already have the latest version of the Selenium Webdriver library, the upgrade command will return the status as up-to-date.
C:\python\python35>pip install -U selenium Requirement already up-to-date: selenium in c:\python\python35\lib\site-packages ←[33mYou are using pip version 7.ane.two, however, version 8.1.0 is bachelor. You should consider upgrading via the 'python -yard pip install --upgrade pip' command.
This arroyo is the safest of all the methods available for installing Selenium with Python. The to a higher place commands volition set up the Selenium WebDriver library on the organization that contains all modules and classes required to create automatic test scripts using Python.
The <pip> tool will download the latest version of the Selenium parcel and install it on your auto.
The optional –U flag will upgrade the existing version of the installed package to the latest version.
B- Build Selenium Jar from Source and install with Python
1- Download the Selenium source lawmaking bundle from the below link. Extract the source into the folder <selenium-2.53.one> and follow the next step.
➡ Download Selenium-2.53.i.tar.gz
2- Now start the command prompt using cmd.exe and run the following command to install Selenium with Python:
C:\python\python35>cd selenium-ii.53.1 C:\python\python35\selenium-2.53.1>python setup.py install
TOC
Selecting Python Editor (IDE)
After we take set up Python and Selenium WebDriver, the side by side important step is to either selection an editor or an IDE (Integrated Development Surroundings) for writing the exam scripts. A adept editor or an IDE makes coding simple with their features. An IDE is even more powerful and comes with some first-class features like:
- A graphical code editor with Code Completion feature.
- Syntax highlighting.
- Code explorer for functions and classes.
- Managing Project construction.
- Reporting and logging tool.
- Tools for Debugging and Unit Testing.
Text editors like Vim, Nano or Notepad, are available to write the Python test scripts.
Simply IDEs are more than convenient to work on large projects, so hither we are giving a cursory overview of some of the famous ones:
Pop Python IDEs for Development
a) PyCharm: Information technology supports Windows, Linux, and Mac operating systems. PyCharm comes in two versions- community edition and professional person edition. You can readily use its Community Edition every bit information technology is free. It has a Professional person version which has more avant-garde features, but you lot've to buy it.
Hence, the Customs edition is suitable for creating and running Selenium examination scripts. It has first-class debugging capabilities. To explore more well-nigh PyCharm and its features accomplish out the following link:
http://world wide web.jetbrains.com/pycharm/
b) PyDev Eclipse plugin: PyDev is Python IDE for Eclipse.PyDev can be installed as a plug-in via the Eclipse update manager using the following update site: http://pydev.org/updates
You tin see the detailed installation instructions at:
http://www.vogella.com/tutorials/Python/article.html
c) PyScripter:PyScripter is gratis and open-source Python IDE for Windows. It isn't compatible with Linux. It provides an extensive alloy of features that modern IDEs offer such as IntelliSense and code completion, testing, and debugging back up. More details almost PyScripter and its download information is bachelor at the following link:
https://lawmaking.google.com/p/pyscripter/
With this, we are fix to create and run our exam scripts using Selenium and Python.
TOC
Prepare Your Starting time Selenium Webdriver Automation Script Using Python
Let'southward start with Selenium WebDriver and create a Python script that uses Selenium classes and functions to automate browser interaction.
Here we volition show you lot a sample script that opens "www.google.com" enters a search text in the Google search text box. Examination script then verifies the Google search page on which has the searched text displayed.
Selenium Webdriver Python Script for Firefox
from selenium import webdriver from selenium.webdriver.common.keys import Keys # create a new Firefox session driver = webdriver.Firefox() driver.implicitly_wait(30) driver.maximize_window() # Navigate to the application abode folio commuter.get("http://www.google.com") # get the search textbox search_field = commuter.find_element_by_id("lst-ib") search_field.articulate() # enter search keyword and submit search_field.send_keys("Selenium WebDriver Interview questions") search_field.submit() # go the listing of elements which are displayed subsequently the search # currently on result page using find_elements_by_class_name method lists= driver.find_elements_by_class_name("_Rm") # get the number of elements establish print ("Found " + str(len(lists)) + " searches:") # iterate through each element and print the text that is # name of the search i=0 for listitem in lists: print (listitem.get_attribute("innerHTML")) i=i+1 if(i>ten): break # shut the browser window driver.quit()
Decoding the to a higher place script
Allow's discuss the script line past line to go a better understanding of the Selenium WebDriver statements in brief. There will be a lot more nigh different Selenium WebDriver functions in upcoming posts.
Stride-i.
The selenium webdriver module implements the classes that support different browsers including Firefox, Chrome, Net Explorer, Safari, others, and RemoteWebDriver likewise to test on browsers available on remote machines.
We demand to import webdriver from the Selenium package to use the Selenium WebDriver methods as:
from selenium import webdriver
Step-ii.
Adjacent, we need the object of the browser which nosotros'll use to load the web page. The browser object provides a programmable interface to communicate with the browser using the Selenium commands. In the test script, we are using Firefox. We can create an case of the Firefox as shown in the post-obit code:
driver = webdriver.Firefox()
On executing this statement, a new Firefox window volition launch. We had fabricated the following settings for the driver instance:
driver.implicitly_wait(30)
driver.maximize_window()
We configured a timeout for Selenium to launch the browser in thirty seconds. Adjacent statement maximizes the browser window.
Step-3.
Side by side, we will navigate to the application, in our case ' http://world wide web.google.com ,' passing the given URL to the driver.become() method. Subsequently making a call to the get() method, Webdriver waits until the page gets rendered in the browser window and sends the control back to the script.
Afterwards the page gets loaded, Selenium will interact with diverse elements on the page. Side by side, in the examination script, we will exist looking at unlike Selenium WebDriver functions that search an HTML object, send a text to the web component, simulate keypress result, click buttons and select from drop downs, etc. Allow's come across all these functions getting used in the next step.
Stride-4.
* First of all, we'll locate the Google Search textbox to supply the text input for the Search. The Search text box has an id attribute as <lst-ib>, and y'all can identify information technology from the lawmaking given below:
search_field = driver.find_element_by_id(<lst-ib>)
* After locating the Search text box, we are trying to interact with the textbox element by clearing the previous value using the clear() method and and so using the send_keys() method to provide a new value. Subsequently calling the submit() method volition forward the search request for processing. You can run into a quick preview of these steps in the adjacent few lines.
search_field.articulate()
search_field.send_keys("Selenium WebDriver Interview questions")
search_field.submit()
* After submitting the search request, Firefox commuter will display the consequence page returned past Google. The issue page shows a listing of entries that match the searched text. Each of the entry in the list is captured in ballast <a> element and can be accessed using "find_elements_by_class_name" method. If used, it volition return a list of elements as:
lists= driver.find_elements_by_class_name("_Rm")
* The list of items expands to many pages, then we are restricting our code to print starting time 10 entries captured in the ballast tag. We are outputting the names of the entries using the "innerHTML" property of the anchor <a> elements:
i=0 for listitem in lists: print (listitem.get_attribute("innerHTML")) i=i+ane if(i>x): suspension
This example gave united states a real insight into using Selenium WebDriver and Python together to create a simple test automation script. It is a very basic instance script. We will use other interesting and complicated features of Selenium Library with Python in our upcoming posts.
TOC
Creating Selenium Webdriver Python Script for Internet Explorer and Google Chrome
Selenium supports cross-browser testing which means nosotros can automate other browsers like Internet Explorer, Google Chrome, Safari and headless browsers like PhantomJS.
In this section, we will reuse the test script created in the before for the Cyberspace Explorer and Google Chrome to verify the cross-browser capabilities of Selenium WebDriver.
Setting up Net Explorer (IE)
To run the Selenium test scripts in the Internet Explorer, you first need to download and set up the InternetExplorerDriver server. This driver is a standalone server executable which enforces the WebDriver'southward wire protocol to piece of work every bit a link between the test script and Internet Explorer browser.
It supports most of the IE versions on Windows XP, Vista, Windows vii and Windows 8 operating systems.
Post-obit are the steps to ready the InternetExplorerDriver server:
i. Download the InternetExplorerDriver server from the mentioned link http://www.seleniumhq.org/download/. Both 32 and 64-scrap versions are available for download. We have to choose based on our organization configuration.
two. Extract the downloaded InternetExplorerDriver server and copy the file to the aforementioned directory where the test scripts reside.
iii. Next, you need to check the Protected Mode setting (in IE 7 or higher). It must take the same value for each security zone. Y'all can keep information technology on or off as long as it is the same for all the zones. Following are the steps to change the Protected Way settings in IE:
a. Choose Internet Options from the Tools menu.
b. From the Internet Options dialog, click on the Security tab.
c. Select each zone listed in " Select a zone to view or change security settings " and make sure that " Enable Protected Mode " (may require restarting your browser) is either on or off for all the zones. It is advisable for all the zones to take similar settings as shown in the image as given below:
iv. Finally, make the following modifications to the examination script to open up the website on Internet Explorer browser:
Selenium Webdriver Python Script for IE
import os from selenium import webdriver from selenium.webdriver.common.keys import Keys # go the path of IEDriverServer dir = os.path.dirname(__file__) ie_driver_path = dir + "\IEDriverServer.exe" # create a new Internet Explorer session driver = webdriver.Ie(ie_driver_path) driver.implicitly_wait(thirty) driver.maximize_window() # Navigate to the awarding dwelling house page commuter.get("http://www.google.com") # go the search textbox search_field = commuter.find_element_by_name("q") # enter search keyword and submit search_field.send_keys("Selenium WebDriver Interview questions") search_field.submit() # get the list of elements which are displayed afterward the search # currently on result page using find_elements_by_class_name method lists= driver.find_elements_by_class_name("r") # get the number of elements found print ("Found " + str(len(lists)) + " searches:") # iterate through each element and print the text that is # name of the search i=0 for listitem in lists: impress (listitem.get_attribute("innerHTML")) i=i+1 if(i>10): break # shut the browser window driver.quit()
Execution steps in the test script created for Internet Explorer are pretty much like to what we did for Firefox.
Hither one important betoken to note is that locators may get changed on different browsers so e'er verify them earlier using in the test scripts created for Selenium Test Automation.
TOC
Setting upward Google Chrome (Chrome)
The above Selenium examination automation script tin can also run on Google Chrome without making too many modifications.
You need to download the ChromeDriver server library similar to the InternetExplorerDriver . The ChromeDriver server is a standalone server, and it implements WebDriver's wire protocol for automating Google Chrome.
This Selenium driver is compatible with Windows, Linux, and Bone X operating systems. Y'all can download the ChromeDriver server using the below steps:
i. Get the latest library of the ChromeDriver from http://chromedriver.storage.googleapis.com/index.html.
ii. Excerpt the downloaded ChromeDriver and copy the file to the same directory where the examination scripts reside.
iii. Finally, brand the following modifications to the test script for opening the website in the Chrome browser:
Selenium Webdriver Python Script for Chrome
import os from selenium import webdriver from selenium.webdriver.common.keys import Keys # get the path of ChromeDriverServer dir = os.path.dirname(__file__) chrome_driver_path = dir + "\chromedriver.exe" # create a new Chrome session commuter = webdriver.Chrome(chrome_driver_path) driver.implicitly_wait(30) driver.maximize_window() # Navigate to the application home page driver.get("http://www.google.com") # go the search textbox search_field = commuter.find_element_by_name("q") # enter search keyword and submit search_field.send_keys("Selenium WebDriver Interview questions") search_field.submit() # get the list of elements which are displayed later the search # currently on result page using find_elements_by_class_name method lists= driver.find_elements_by_class_name("r") # get the number of elements found print ("Institute " + str(len(lists)) + " searches:") # iterate through each element and print the text that is # name of the search i=0 for listitem in lists: print (listitem.get_attribute("innerHTML")) i=i+1 if(i>10): break # close the browser window driver.quit()
Upon executing the above test script, Selenium will first launch the Chromedriver server, which starts the Chrome browser and performs the steps. This execution is very like to what you've seen with Firefox and the Internet Explorer above.
TOC
Summary – Selenium Webdriver Python Tutorial
We've tried to brand this Selenium Webdriver Python tutorial as intuitive every bit nosotros could. Multiple examples that we've added can assistance you to get a quick heads-up on while working with Selenium using Python.
However, if you like to go to the next level and wish to create a examination automation suite in Selenium Python, then do follow the below tutorial.
Delight write to us about the Selenium Webdriver Python tutorial and share your views. Too please share this post on social media, employ the share icons just beneath the post.
Keep Learning and Go along Practicing for Improve Results.
-TechBeamers
Source: https://www.techbeamers.com/selenium-webdriver-python-tutorial/
Posted by: craigponly1999.blogspot.com
0 Response to "How To Upload Files Using Selenium Webdriver Python"
Post a Comment