Chrome WebDriver¶
Chrome WebDriver is provided by Selenium2. To use it, you need to install Selenium2 via pip:
$ [sudo] pip install selenium
It’s important to note that you also need to have Google Chrome installed in your machine.
Chrome can also be used from a custom path. To do this pass the executable path as a dictionary to the **kwargs argument. The dictionary should be set up with executable_path as the key and the value set to the path to the executable file.
from splinter import Browser
executable_path = {'executable_path':'</path/to/chrome>'}
browser = Browser('chrome', **executable_path)
Setting up Chrome WebDriver¶
In order to use Google Chrome with Splinter, since we’re using Selenium 2.3.x, you need to setup Chrome webdriver properly.
Linux¶
Go to the download page on the Chromium project and choose
the correct version for your Linux installation. Then extract the downloaded file in a
directory in the PATH
(e.g. /usr/bin
). You can also extract it to any
directory and add that directory to the PATH
:
Linux 64bits¶
$ cd $HOME/Downloads
$ wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
$ unzip chromedriver_linux64.zip
$ mkdir -p $HOME/bin
$ mv chromedriver $HOME/bin
$ echo "export PATH=$PATH:$HOME/bin" >> $HOME/.bash_profile
Windows¶
Note: We don’t provide official support for Windows, but you can try it by yourself.
All you need to do is go to download page on Selenium project and choose
“ChromeDriver server for win”. Your browser will download a zip file, extract it and add the .exe
file to your PATH.
If you don’t know how to add an executable to the PATH on Windows, check these link out:
Using Chrome WebDriver¶
To use the Chrome driver, all you need to do is pass the string chrome
when you create
the Browser
instance:
from splinter import Browser
browser = Browser('chrome')
Note: if you don’t provide any driver to the Browser
function, firefox
will be used.
Note: if you have trouble with $HOME/.bash_profile
, you can try $HOME/.bashrc
.
Using headless option for Chrome¶
Starting with Chrome 59, we can run Chrome as a headless browser. Make sure you read google developers updates
from splinter import Browser
browser = Browser('chrome', headless=True)
Using incognito option for Chrome¶
We can run Chrome as a incognito browser.
from splinter import Browser
browser = Browser('chrome', incognito=True)
Using emulation mode in Chrome¶
Chrome options can be passed to customize Chrome’s behaviour; it is then possible to leverage the experimental emulation mode.
from selenium import webdriver
from splinter import Browser
mobile_emulation = {"deviceName": "Google Nexus 5"}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation",
mobile_emulation)
browser = Browser('chrome', options=chrome_options)
refer to chrome driver documentation
API docs¶
-
class
splinter.driver.webdriver.chrome.
WebDriver
(options=None, user_agent=None, wait_time=2, fullscreen=False, incognito=False, headless=False, **kwargs)¶ -
attach_file
(name, value)¶ Fill the field identified by
name
with the content specified byvalue
.
-
back
()¶ Back to the last URL in the browsing history.
If there is no URL to back, this method does nothing.
-
check
(name)¶ Checks a checkbox by its name.
Example:
>>> browser.check("agree-with-terms")
If you call
browser.check
n times, the checkbox keeps checked, it never get unchecked.To unckech a checkbox, take a look in the
uncheck
method.
-
choose
(name, value)¶ Chooses a value in a radio buttons group.
Suppose you have the two radio buttons in a page, with the name
gender
and values ‘F’ and ‘M’. If you use thechoose
method the following way:>>> browser.choose('gender', 'F')
Then you’re choosing the female gender.
-
click_link_by_href
(href)¶ Clicks in a link by its
href
attribute.
-
click_link_by_id
(id)¶ Clicks in a link by id.
-
click_link_by_partial_href
(partial_href)¶ Clicks in a link by looking for partial content of
href
attribute.
-
click_link_by_partial_text
(partial_text)¶ Clicks in a link by partial content of its text.
-
click_link_by_text
(text)¶ Clicks in a link by its
text
.
A
CookieManager
instance.For more details, check the cookies manipulation section.
-
evaluate_script
(script, *args)¶ Similar to
execute_script
method.Executes javascript in the browser and returns the value of the expression.
- e.g.: ::
>>> assert 4 == browser.evaluate_script('2 + 2')
-
execute_script
(script, *args)¶ Executes a given JavaScript in the browser.
- e.g.: ::
>>> browser.execute_script('document.getElementById("body").innerHTML = "<p>Hello world!</p>"')
-
fill
(name, value)¶ Fill the field identified by
name
with the content specified byvalue
.
-
fill_form
(field_values, form_id=None, name=None)¶ Fill the fields identified by
name
with the content specified byvalue
in a dict.Currently, fill_form supports the following fields: text, password, textarea, checkbox, radio and select.
Checkboxes should be specified as a boolean in the dict.
-
find_by_css
(css_selector)¶ Returns an instance of
ElementList
, using a CSS selector to query the current page content.
-
find_by_id
(id)¶ Finds an element in current page by its id.
Even when only one element is find, this method returns an instance of
ElementList
-
find_by_name
(name)¶ Finds elements in current page by their name.
Returns an instance of
ElementList
.
-
find_by_tag
(tag)¶ Find all elements of a given tag in current page.
Returns an instance of
ElementList
-
find_by_text
(text)¶ Finds elements in current page by their text.
Returns an instance of
ElementList
-
find_by_value
(value)¶ Finds elements in current page by their value.
Returns an instance of
ElementList
-
find_by_xpath
(xpath, original_find=None, original_query=None)¶ Returns an instance of
ElementList
, using a xpath selector to query the current page content.
-
find_link_by_href
(href)¶ Find all elements of a given tag in current page.
Returns an instance of
ElementList
-
find_link_by_partial_href
(partial_href)¶ Find links by looking for a partial
str
in their href attribute.Returns an instance of
ElementList
-
find_link_by_partial_text
(partial_text)¶ Find links by looking for a partial
str
in their text.Returns an instance of
ElementList
-
find_link_by_text
(text)¶ Find links querying for their text.
Returns an instance of
ElementList
-
find_option_by_text
(text)¶ Finds
<option>
elements by their text.Returns an instance of
ElementList
-
find_option_by_value
(value)¶ Finds
<option>
elements by their value.Returns an instance of
ElementList
-
forward
()¶ Forward to the next URL in the browsing history.
If there is no URL to forward, this method does nothing.
-
get_alert
()¶ Changes the context for working with alerts and prompts.
For more details, check the docs about iframes, alerts and prompts
-
get_iframe
(**kwds)¶ Changes the context for working with iframes.
For more details, check the docs about iframes, alerts and prompts
-
html
¶ Source of current page.
-
is_element_not_present_by_css
(css_selector, wait_time=None)¶ Verify if the element is not present in the current page by css, and wait the specified time in
wait_time
.Returns True if the element is not present and False if is present.
-
is_element_not_present_by_id
(id, wait_time=None)¶ Verify if the element is present in the current page by id, and wait the specified time in
wait_time
.Returns True if the element is not present and False if is present.
-
is_element_not_present_by_name
(name, wait_time=None)¶ Verify if the element is not present in the current page by name, and wait the specified time in
wait_time
.Returns True if the element is not present and False if is present.
-
is_element_not_present_by_tag
(tag, wait_time=None)¶ Verify if the element is not present in the current page by tag, and wait the specified time in
wait_time
.Returns True if the element is not present and False if is present.
-
is_element_not_present_by_text
(text, wait_time=None)¶ Verify if the element is not present in the current page by text, and wait the specified time in
wait_time
.Returns True if the element is not present and False if is present.
-
is_element_not_present_by_value
(value, wait_time=None)¶ Verify if the element is not present in the current page by value, and wait the specified time in
wait_time
.Returns True if the element is not present and False if is present.
-
is_element_not_present_by_xpath
(xpath, wait_time=None)¶ Verify if the element is not present in the current page by xpath, and wait the specified time in
wait_time
.Returns True if the element is not present and False if is present.
-
is_element_present_by_css
(css_selector, wait_time=None)¶ Verify if the element is present in the current page by css, and wait the specified time in
wait_time
.Returns True if the element is present and False if is not present.
-
is_element_present_by_id
(id, wait_time=None)¶ Verify if the element is present in the current page by id, and wait the specified time in
wait_time
.Returns True if the element is present and False if is not present.
-
is_element_present_by_name
(name, wait_time=None)¶ Verify if the element is present in the current page by name, and wait the specified time in
wait_time
.Returns True if the element is present and False if is not present.
-
is_element_present_by_tag
(tag, wait_time=None)¶ Verify if the element is present in the current page by tag, and wait the specified time in
wait_time
.Returns True if the element is present and False if is not present.
-
is_element_present_by_text
(text, wait_time=None)¶ Verify if the element is present in the current page by text, and wait the specified time in
wait_time
.Returns True if the element is present and False if is not present.
-
is_element_present_by_value
(value, wait_time=None)¶ Verify if the element is present in the current page by value, and wait the specified time in
wait_time
.Returns True if the element is present and False if is not present.
-
is_element_present_by_xpath
(xpath, wait_time=None)¶ Verify if the element is present in the current page by xpath, and wait the specified time in
wait_time
.Returns True if the element is present and False if is not present.
-
is_text_present
(text, wait_time=None)¶ Searchs for
text
in the browser and wait the seconds specified inwait_time
.Returns True if finds a match for the
text
and False if not.
-
quit
()¶ Quits the browser, closing its windows (if it has one).
After quit the browser, you can’t use it anymore.
-
reload
()¶ Revisits the current URL
-
screenshot
(name='', suffix='.png', full=False)¶ Takes a screenshot of the current page and saves it locally.
-
select
(name, value)¶ Selects an
<option>
element in an<select>
element using thename
of the<select>
and thevalue
of the<option>
.Example:
>>> browser.select("state", "NY")
-
title
¶ Title of current page.
-
type
(name, value, slowly=False)¶ Types the
value
in the field identified byname
.It’s useful to test javascript events like keyPress, keyUp, keyDown, etc.
If
slowly
is True, this function returns an iterator which will type one character per iteration.
-
uncheck
(name)¶ Unchecks a checkbox by its name.
Example:
>>> browser.uncheck("send-me-emails")
If you call
brower.uncheck
n times, the checkbox keeps unchecked, it never get checked.To check a checkbox, take a look in the
check
method.
-
url
¶ URL of current page.
-
visit
(url)¶ Visits a given URL.
The
url
parameter is a string.
-