8. Appendix: Frequently Asked Questions

Another FAQ: https://github.com/SeleniumHQ/selenium/wiki/Frequently-Asked-Questions

8.1. How to use ChromeDriver ?

Download the latest chromedriver from download page. Unzip the file:

unzip chromedriver_linux64.zip

You should see a chromedriver executable. Now you can create an instance of Chrome WebDriver like this:

driver = webdriver.Chrome(executable_path="/path/to/chromedriver")

The rest of the example should work as given in other documentation.

8.2. Does Selenium 2 support XPath 2.0 ?

Ref: http://seleniumhq.org/docs/03_webdriver.html#how-xpath-works-in-webdriver

Selenium delegates XPath queries down to the browser’s own XPath engine, so Selenium support XPath supports whatever the browser supports. In browsers which don’t have native XPath engines (IE 6,7,8), Selenium supports XPath 1.0 only.

8.3. How to scroll down to the bottom of a page ?

Ref: http://blog.varunin.com/2011/08/scrolling-on-pages-using-selenium.html

You can use the execute_script method to execute javascript on the loaded page. So, you can call the JavaScript API to scroll to the bottom or any other position of a page.

Here is an example to scroll to the bottom of a page:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

The window object in DOM has a scrollTo method to scroll to any position of an opened window. The scrollHeight is a common property for all elements. The document.body.scrollHeight will give the height of the entire body of the page.

8.4. How to auto save files using custom Firefox profile ?

Ref: http://stackoverflow.com/questions/1176348/access-to-file-download-dialog-in-firefox

Ref: http://blog.codecentric.de/en/2010/07/file-downloads-with-selenium-mission-impossible/

The first step is to identify the type of file you want to auto save.

To identify the content type you want to download automatically, you can use curl:

curl -I URL | grep "Content-Type"

Another way to find content type is using the requests module, you can use it like this:

import requests
content_type = requests.head('http://www.python.org').headers['content-type']

Once the content type is identified, you can use it to set the firefox profile preference: browser.helperApps.neverAsk.saveToDisk

Here is an example:

import os

from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")

browser = webdriver.Firefox(firefox_profile=fp)

In the above example, application/octet-stream is used as the content type.

The browser.download.dir option specify the directory where you want to download the files.

8.5. How to upload files into file inputs ?

Select the <input type="file"> element and call the send_keys() method passing the file path, either the path relative to the test script, or an absolute path. Keep in mind the differences in path names between Windows and Unix systems.

8.6. How to use firebug with Firefox ?

First download the Firebug XPI file, later you call the add_extension method available for the firefox profile:

from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("extensions.firebug.currentVersion", "1.8.4") #Avoid startup screen
browser = webdriver.Firefox(firefox_profile=fp)

8.7. How to take screenshot of the current window ?

Use the save_screenshot method provided by the webdriver:

from selenium import webdriver

driver = webdriver.Firefox()