Selenium with Python¶
This is not an official documentation. If you would like to contribute to this documentation, you can fork this project in GitHub and send pull requests. You can also send your feedback to my email: baiju.m.mail AT gmail DOT com. So far 60+ community members have contributed to this project (See the closed pull requests). I encourage contributors to add more sections and make it an awesome documentation! If you know any translation of this document, please send a PR to update the below list.
Translations:
- 1. Installation
- 1.1. Introduction
- 1.2. Installing Python bindings for Selenium
- 1.3. Instructions for Windows users
- 1.4. Installing from Git sources
- 1.5. Drivers
- 1.6. Downloading Selenium server
- 2.1. Simple Usage
- 2.2. Example Explained
- 2.3. Using Selenium to write tests
- 2.4. Walkthrough of the example
- 2.5. Using Selenium with remote WebDriver
- 3.1. Interacting with the page
- 3.2. Filling in forms
- 3.3. Drag and drop
- 3.4. Moving between windows and frames
- 3.5. Popup dialogs
- 3.6. Navigation: history and location
- 3.7. Cookies
- 4.1. Locating by Id
- 4.2. Locating by Name
- 4.3. Locating by XPath
- 4.4. Locating Hyperlinks by Link Text
- 4.5. Locating Elements by Tag Name
- 4.6. Locating Elements by Class Name
- 4.7. Locating Elements by CSS Selectors
- 5.1. Explicit Waits
- 5.2. Implicit Waits
- 6.1. Test case
- 6.2. Page object classes
- 6.3. Page elements
- 6.4. Locators
- 7.1. Exceptions
- 7.2. Action Chains
- 7.3. Alerts
- 7.4. Special Keys
- 7.5. Locate elements By
- 7.6. Desired Capabilities
- 7.7. Touch Actions
- 7.8. Proxy
- 7.9. Utilities
- 7.10. Service
- 7.11. Application Cache
- 7.12. Firefox WebDriver
- 7.13. Firefox WebDriver Options
- 7.14. Firefox WebDriver Profile
- 7.15. Firefox WebDriver Binary
- 7.16. Firefox WebDriver Extension Connection
- 7.17. Chrome WebDriver
- 7.18. Chrome WebDriver Options
- 7.19. Chrome WebDriver Service
- 7.20. Remote WebDriver
- 7.21. Remote WebDriver WebElement
- 7.22. Remote WebDriver Command
- 7.23. Remote WebDriver Error Handler
- 7.24. Remote WebDriver Mobile
- 7.25. Remote WebDriver Remote Connection
- 7.26. Remote WebDriver Utils
- 7.27. Internet Explorer WebDriver
- 7.28. Android WebDriver
- 7.29. Opera WebDriver
- 7.30. PhantomJS WebDriver
- 7.31. PhantomJS WebDriver Service
- 7.32. Safari WebDriver
- 7.33. Safari WebDriver Service
- 7.34. Select Support
- 7.35. Wait Support
- 7.36. Color Support
- 7.37. Event Firing WebDriver Support
- 7.38. Abstract Event Listener Support
- 7.39. Expected conditions Support
- 8.1. How to use ChromeDriver ?
- 8.2. Does Selenium 2 support XPath 2.0 ?
- 8.3. How to scroll down to the bottom of a page ?
- 8.4. How to auto save files using custom Firefox profile ?
- 8.5. How to upload files into file inputs ?
- 8.6. How to use firebug with Firefox ?
- 8.7. How to take screenshot of the current window ?
3. Navigating¶
The first thing you’ll want to do with WebDriver is navigate to a link. The normal way to do this is by calling get method:
driver.get("http://www.google.com")
WebDriver will wait until the page has fully loaded (that is, the onload event has fired) before returning control to your test or script. Be aware that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded. If you need to ensure such pages are fully loaded then you can use waits .
3.1. Interacting with the page¶
Just being able to go to places isn’t terribly useful. What we’d really like to do is to interact with the pages, or, more specifically, the HTML elements within a page. First of all, we need to find one. WebDriver offers a number of ways to find elements. For example, given an element defined as:
input type="text" name="passwd" id="passwd-id" />
you could find it using any of:
element = driver.find_element(By.ID, "passwd-id") element = driver.find_element(By.NAME, "passwd") element = driver.find_element(By.XPATH, "//input[@id='passwd-id']") element = driver.find_element(By.CSS_SELECTOR, "input#passwd-id")
You can also look for a link by its text, but be careful! The text must be an exact match! You should also be careful when using XPATH in WebDriver . If there’s more than one element that matches the query, then only the first will be returned. If nothing can be found, a NoSuchElementException will be raised.
WebDriver has an “Object-based” API; we represent all types of elements using the same interface. This means that although you may see a lot of possible methods you could invoke when you hit your IDE’s auto-complete key combination, not all of them will make sense or be valid. Don’t worry! WebDriver will attempt to do the Right Thing, and if you call a method that makes no sense (“setSelected()” on a “meta” tag, for example) an exception will be raised.
So, you’ve got an element. What can you do with it? First of all, you may want to enter some text into a text field:
element.send_keys("some text")
You can simulate pressing the arrow keys by using the “Keys” class:
element.send_keys(" and some", Keys.ARROW_DOWN)
It is possible to call send_keys on any element, which makes it possible to test keyboard shortcuts such as those used on GMail. A side-effect of this is that typing something into a text field won’t automatically clear it. Instead, what you type will be appended to what’s already there. You can easily clear the contents of a text field or textarea with the clear method:
3.2. Filling in forms¶
We’ve already seen how to enter text into a textarea or text field, but what about the other elements? You can “toggle” the state of the drop down, and you can use “setSelected” to set something like an OPTION tag selected. Dealing with SELECT tags isn’t too bad:
element = driver.find_element(By.XPATH, "//select[@name='name']") all_options = element.find_elements(By.TAG_NAME, "option") for option in all_options: print("Value is: %s" % option.get_attribute("value")) option.click()
This will find the first “SELECT” element on the page, and cycle through each of its OPTIONs in turn, printing out their values, and selecting each in turn.
As you can see, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called a “Select”, which provides useful methods for interacting with these:
from selenium.webdriver.support.ui import Select select = Select(driver.find_element(By.NAME, 'name')) select.select_by_index(index) select.select_by_visible_text("text") select.select_by_value(value)
WebDriver also provides features for deselecting all the selected options:
select = Select(driver.find_element(By.ID, 'id')) select.deselect_all()
This will deselect all OPTIONs from that particular SELECT on the page.
Suppose in a test, we need the list of all default selected options, Select class provides a property method that returns a list:
select = Select(driver.find_element(By.XPATH, "//select[@name='name']")) all_selected_options = select.all_selected_options
To get all available options:
Once you’ve finished filling out the form, you probably want to submit it. One way to do this would be to find the “submit” button and click it:
# Assume the button has the ID "submit" :) driver.find_element(By.ID, "submit").click()
Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be raised:
3.3. Drag and drop¶
You can use drag and drop, either moving an element by a certain amount, or on to another element:
element = driver.find_element(By.NAME, "source") target = driver.find_element(By.NAME, "target") from selenium.webdriver import ActionChains action_chains = ActionChains(driver) action_chains.drag_and_drop(element, target).perform()
3.4. Moving between windows and frames¶
It’s rare for a modern web application not to have any frames or to be constrained to a single window. WebDriver supports moving between named windows using the “switch_to.window” method:
driver.switch_to.window("windowName")
All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window’s name? Take a look at the javascript or link that opened it:
a href="somewhere.html" target="windowName">Click here to open a new windowa>
Alternatively, you can pass a “window handle” to the “switch_to.window()” method. Knowing this, it’s possible to iterate over every open window like so:
for handle in driver.window_handles: driver.switch_to.window(handle)
You can also swing from frame to frame (or into iframes):
driver.switch_to.frame("frameName")
It’s possible to access subframes by separating the path with a dot, and you can specify the frame by its index too. That is:
driver.switch_to.frame("frameName.0.child")
would go to the frame named “child” of the first subframe of the frame called “frameName”. All frames are evaluated as if from *top*.
Once we are done with working on frames, we will have to come back to the parent frame which can be done using:
driver.switch_to.default_content()
3.5. Popup dialogs¶
Selenium WebDriver has built-in support for handling popup dialog boxes. After you’ve triggered action that would open a popup, you can access the alert with the following:
alert = driver.switch_to.alert
This will return the currently open alert object. With this object, you can now accept, dismiss, read its contents or even type into a prompt. This interface works equally well on alerts, confirms, prompts. Refer to the API documentation for more information.
3.6. Navigation: history and location¶
Earlier, we covered navigating to a page using the “get” command ( driver.get(«http://www.example.com») ). As you’ve seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task. To navigate to a page, you can use get method:
driver.get("http://www.example.com")
To move backward and forward in your browser’s history:
Please be aware that this functionality depends entirely on the underlying driver. It’s just possible that something unexpected may happen when you call these methods if you’re used to the behavior of one browser over another.
3.7. Cookies¶
Before moving to the next section of the tutorial, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for:
# Go to the correct domain driver.get("http://www.example.com") # Now set the cookie. This one's valid for the entire domain cookie = 'name' : 'foo', 'value' : 'bar'> driver.add_cookie(cookie) # And now output all the available cookies for the current URL driver.get_cookies()