I present the translation of the unofficial Selenium documentation for Python.
The translation was made with the permission of the author Baiju Muthukadan.
The original can be found
here .
Preface from the author of the article
Selenium WebDriver is a software library for managing browsers. WebDriver provides drivers for various browsers and client libraries in various programming languages ​​designed to manage these drivers.
In essence, the use of such a web driver is reduced to creating a bot that performs all manual work with the browser automatically.
')
WebDriver libraries are available in Java, .Net (C #), Python, Ruby, JavaScript, drivers are implemented for Firefox, InternetExplorer, Safari, Andriod, iOS (as well as Chrome and Opera) browsers.
Scope of applicationMost often, Selenium WebDriver is used to test the functionality of websites / web-based applications. Automated testing is convenient because it allows you to repeatedly run repeated tests. Regression testing, that is, checking that the old code did not stop working properly after making new changes, is a typical example when automation is needed. WebDriver provides all the necessary methods, provides a high speed test and ensures correctness of the test (since the human factor is excluded). The official Selenium documentation contains the following advantages of automated testing of web applications:
- the ability to conduct more regression testing;
- fast provision to developers of a report on the state of the product;
- obtaining a potentially infinite number of test runs;
- providing support for Agile and extreme development methods;
- maintaining strict test documentation;
- detection of errors that were missed at the stage of manual testing.
WebDriver functionality allows you to use it not only for testing, but also for administering web services, reducing to the possible limit the number of manual actions. Selenium WebDriver becomes an indispensable assistant in cases where, for example, the core of the site is outdated and requires a lot of body movements from moderators to implement a small feature (for example, uploading a photo gallery).
Also one of the indispensable features of Selenium WebDriver is the waiting page load. This could include cases where data parsing on a page is impossible due to redirection pages or waiting pages that contain something like this: “Wait, the page is loading.” Such pages, of course, is not the purpose of parsing, but it is often not possible to bypass them. Naturally, without Selenium WebDriver. Selenium WebDriver allows in such cases to “wait”, as a person would expect, until an element with the necessary name appears on the page, for example.
Another advantage of Selenium is that the actions of the web driver are visually visible and require minimal time spent on the page, this allows you to comfortably demonstrate the functionality of the site when a presentation of the service is needed.
Some problems WebDriver (from the network and personal experience):
- it happens that the behavior is different in different browsers;
- Sometimes it is difficult to find elements (XPath and other methods sometimes just do not work, although they should);
- unexplained driver crashes right in the middle of the test;
- interaction is possible only with the first tab of the browser, the driver allows you to open new tabs and new windows, but does not allow them to work;
- it is necessary to clearly think out the architecture of the test, often use assert or expectations so that the test can “think” when to do and when not.
More information about the Selenium project and their software series can be found
here , either in the
official documentation or
its translation .
Content:
1. Installation
2.
First Steps3.
Navigation4.
Search for Items5.
Expectations6. Page
Objects7. WebDriver API
8. Appendix: Frequently Asked Questions
1. Installation
1.1. Introduction
Selenium binding to Python provides a simple API [Application Programming Interface (eng. Application Programming Interface) - Approx. trans.] to write functionality tests / compliance tests using the Selenium WebDriver web driver. With the help of the Selenium Python API, you can intuitively simply access all the functionality of Selenium WebDriver.
The Python-Selenium binding provides a convenient API for accessing Selenium web drivers such as Firefox, Ie, Chrome, Remote and others. Currently, Python versions 2.7, 3.2, 3.3 and 3.4 are supported.
This documentation covers the Selenium 2 WebDriver API. Selenium 1 / Selenium RC API is not covered.
1.2. Download Selenium for Python
You can download Selenium to Python bindings from the
selenium package page on PyPI . However, the best way is to use the
pip module. Python 3.4 contains pip in the
standard library . Using pip, you can install selenium with the following command:
pip install selenium
You can use
virtualenv to create an isolated Python environment. The Python 3.4 library also contains the
pyvenv module, which is almost the same as virtualenv.
1.3. Detailed instructions for Windows users
Note
For this installation you need access to the Internet.
1. Install Python 3.4 via the
MSI file available on the download page of the python.org site .
2. Run the command line through the cmd.exe program and run the selenium pip installation command, as shown below.
C: \ Python34 \ Scripts \ pip.exe install selenium
Now you can run your test scripts using Python. For example, if you created a script based on Selenium and saved it in C: \ my_selenium_script.py, then you can run it with the following command:
C: \ Python34 \ python.exe C: \ my_selenium_script.py
1.4. Selenium server download
Note
Selenium server is required in cases when you want to use remote WebDriver [remote - Approx. trans.]. For more information, see Using Selenium with remote WebDriver. If you are just starting to learn Selenium, you can skip this section and continue with the next chapter.
Selenium server is written in Java. A Java Runtime Environment (JRE) version 1.6 or higher is recommended for its launch.
You can download the Selenium server 2.x on
the selenium website download page . The file name should look something like this: selenium-server-standalone-2.xxjar. You can always download the latest version of Selenium server 2.x.
If the Java Runtime Environment (JRE) is not installed on your system, you can download the
JRE from the Oracle website . If you are using GNU / Linux systems and have root rights [administrator rights - Approx. trans.], you can also install the JRE using your system instructions.
If the java command is available in the PATH (environment variable), you can start the Selenium server using the following command:
java -jar selenium-server-standalone-2.xxjar
Replace 2.xx with the latest version of the Selenium server you downloaded from the site.
If the JRE is installed under a user who does not have root rights and / or if it is not available in the PATH environment variable, you can enter a relative or full path to the java file. Similarly, you can add the name of the Selenium server jar file to a relative or full path. After that, the command will look like this:
/ path / to / java -jar /path/to/selenium-server-standalone-2.xxjar
Go to the next chapter.