Selenium for Python. Chapter 1. Installation

Introducing the translation of Selenium informal documentation for Python.
Translated with permission from 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 is a driver for various browsers and client libraries in different programming languages ​​designed to manage these drivers.

In essence, the use of such a web driver comes down to creating a bot that performs all the manual work with the browser automatically.

WebDriver libraries are available in Java, .Net (C #), Python, Ruby, JavaScript, drivers are implemented for the browsers Firefox, InternetExplorer, Safari, Andriod, iOS (as well as Chrome and Opera).

Scope of application
Most often, Selenium WebDriver is used to test the functionality of websites / web-based applications. Automated testing is convenient because it allows you to run repeated tests multiple times. Regression testing, that is, checking that the old code did not stop working correctly after making new changes, is a typical example when automation is needed. WebDriver provides all the necessary methods, provides a high test speed and ensures the validation of the test (since the human factor is excluded). In the official Selenium documentation, the following advantages of automated testing of web applications are listed:

  • the ability to conduct more often regression testing;
  • Rapid reporting of product status to developers
  • Get 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.

The functionality of WebDriver allows you to use it not only for testing, but also for the administration of web services, reducing the number of manual actions to a possible limit. Selenium WebDriver becomes an indispensable tool in cases where, for example, the site’s core is outdated and requires a lot of gestures from moderators to implement a small feature (for example, uploading a photo gallery).

Also one of the indispensable features of Selenium WebDriver is waiting for the page to load. This can include cases when parsing data on a page is not possible due to redirect or wait pages containing approximately the following text: “Wait, the page is loading”. Such pages, needless to say, are not the purpose of parsing, but it is often not possible to get around them. Naturally, without Selenium WebDriver. Selenium WebDriver allows you to “wait” in such cases, as a person would expect, until, for example, an element with the necessary name appears on the page.

Another plus of Selenium is that the actions of the web driver are visually visible and require a minimum time spent on the page, this allows you to conveniently demonstrate the functionality of the site when you need a presentation of the service.

Some WebDriver issues (from the web and personal experience):

  • It happens that the behavior is different in different browsers;
  • sometimes there are difficulties with finding 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 browser tab, the driver allows you to open new tabs and new windows, but does not allow you to work in them;
  • it is necessary to clearly think over the architecture of the test, often use assert or expectations, so that the test can "think" when to do and when not.

You can read more about the Selenium project and a series of their software here , either in the official documentation or its translation .

Content:


1. Installation
2. First Steps
3. Navigation
4. Finding Elements
5. Expectations
6. Page Objects
7. WebDriver API
8. Application: Frequently Asked Questions

1. Installation


1.1. Introduction


Selenium binding to Python provides a simple API [Application Programming Interface] - Note. Per.] for writing functionality tests / compliance tests using the Selenium WebDriver web driver. With the Selenium Python API, you can intuitively 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. Python versions 2.7, 3.2, 3.3, and 3.4 are currently supported.

This documentation covers the Selenium 2 WebDriver API. Selenium 1 / Selenium RC APIs are not covered in it.

1.2. Download Selenium for Python


You can download the Selenium binding to Python from the selenium package page on PyPI . However, the best way would be 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 a Python sandbox . 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 through the MSI file, available on the python.org download page .
2. Run the command line through the cmd.exe program and run the pip install selenium 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. Download Selenium server


Note The
Selenium server is required in cases where you want to use remote WebDriver. 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. To run it, the recommended Java Runtime Environment (JRE) version 1.6 or higher.

You can download Selenium server 2.x on the selenium 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 privileges [administrator rights - approx. per.], you can also install JRE using your system instructions.

If the java command is available in 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 current version of Selenium server that you downloaded from the site.

If the JRE is installed under a non-root user and / or if it is not available in the PATH environment variable, you can enter the 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 next chapter

Also popular now: