8.Appendix: Frequently Asked Questions

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

8.1.How to use ChromeDriver ?

Download the latestchromedriver from download page. Unzip thefile:

unzipchromedriver_linux64.zip

You should see achromedriver executable. Now you can create an instance ofChrome 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, soSelenium support XPath supports whatever the browser supports. In browserswhich don’t have native XPath engines (IE 6,7,8), Selenium supports XPath 1.0only.

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 theexecute_script method to execute javascript on the loadedpage. So, you can call the JavaScript API to scroll to the bottom or any otherposition of a page.

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

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

Thewindow object in DOM hasascrollTo method toscroll to any position of an opened window. ThescrollHeight is a common property for allelements. Thedocument.body.scrollHeight will give the height of the entirebody 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, youcan usecurl:

curl-IURL|grep"Content-Type"

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

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

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

Here is an example:

importosfromseleniumimportwebdriverfp=webdriver.FirefoxProfile()fp.set_preference("browser.download.folderList",2)fp.set_preference("browser.download.manager.showWhenStarting",False)fp.set_preference("browser.download.dir",os.getcwd())fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")browser=webdriver.Firefox(firefox_profile=fp)browser.get("http://pypi.python.org/pypi/selenium")browser.find_element_by_partial_link_text("selenium-2").click()

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

Thebrowser.download.dir option specify the directory where you want todownload the files.

8.5.How to upload files into file inputs ?

Select the<inputtype="file"> element and call thesend_keys() methodpassing the file path, either the path relative to the test script, or anabsolute path. Keep in mind the differences in path names between Windows andUnix systems.

8.6.How to use firebug with Firefox ?

First download the Firebug XPI file, later you call theadd_extension methodavailable for the firefox profile:

fromseleniumimportwebdriverfp=webdriver.FirefoxProfile()fp.add_extension(extension='firebug-1.8.4.xpi')fp.set_preference("extensions.firebug.currentVersion","1.8.4")#Avoid startup screenbrowser=webdriver.Firefox(firefox_profile=fp)

8.7.How to take screenshot of the current window ?

Use thesave_screenshot method provided by the webdriver:

fromseleniumimportwebdriverdriver=webdriver.Firefox()driver.get('http://www.python.org/')driver.save_screenshot('screenshot.png')driver.quit()