splinter 学习笔记

2019-08-02 15:48:52 浏览数 (1)

splinter 学习笔记

1、install

Install Python

In order to install Splinter, make sure Python is installed. Note: only Python 2.7 is supported. 目前splinter支持Python2.7以上的版本,请在对应官网下载 http://www.python.org. Linux 和 Mac OS X有可能已经安装了对应的python

Install splinter

Splinter有两种安装方法如下:

Install a stable release

稳定的版本安装方法

代码语言:javascript复制
$ [sudo] pip install splinter

Install under-development source-code

最新版本但不一定是最稳定的版本安装方法

代码语言:javascript复制
$ git clone git://github.com/cobrateam/splinter.git
$ cd splinter
$ [sudo] python setup.py install

2、支持不同浏览器

2.1Firefox WebDriver

firefox WebDriver是依赖Selenium 2.0的,因此需要安装Selenium 2.0,方法如下:

代码语言:javascript复制
$ [sudo] pip install selenium

PS:firefox也是需要的啊

Using Firefox WebDriver

通过以下方法create一个firefox的实例:

代码语言:javascript复制
from splinter import Browser
browser = Browser('firefox')

PS:Browser()中不指定,默认还是firefox

How to use a specific profile for Firefox

You can specify a Firefox profile for using on Browser function using the profile keyword (passing the name of the profile as a str instance):

代码语言:javascript复制
from splinter import Browser
browser = Browser('firefox', profile='my_profile')

If you don’t specify a profile, a new temporary profile will be created (and deleted when you close the browser).

How to use specific extensions for Firefox

An extension for firefox is a .xpi archive. To use an extension in Firefox webdriver profile you need to give the path of the extension, using the extensions keyword (passing the extensions as a list instance):

代码语言:javascript复制
from splinter import Browser
browser = Browser('firefox', extensions=['extension1.xpi', 'extension2.xpi'])

If you give an extension, after you close the browser, the extension will be deleted from the profile, even if is not a temporary one.

How to use selenium capabilities for Firefox

from splinter import Browser browser = Browser(‘firefox’, capabilities={‘acceptSslCerts’: True}) You can pass any selenium read-write DesiredCapabilities parameters for Firefox.

2.2Chrome WebDriver

To use the Chrome driver, all you need to do is pass the string chrome when you create the Browser instance:

代码语言:javascript复制
from splinter import Browser
browser = Browser('chrome')

Note: if you don’t provide any driver to Browser function, firefox will be used.

Note: if you have trouble with $HOME/.bash_profile, you can try $HOME/.bashrc.

2.3 Remote Driver

Remote WebDriver is provided by Selenium2. To use it, you need to install Selenium2 via pip: $ [sudo] pip install selenium

Setting up the Remote WebDriver

To use the remote web driver, you need to have access to a Selenium remote webdriver server. Setting up one of these servers is beyond the scope of this document. However, some companies provide access to a Selenium Grid as a service.

Using the Remote WebDriver

To use the Remote WebDriver, you need to pass driver_name="remote" and url=when you create the Browser instance.

You can also pass additional arguments that correspond to Selenium DesiredCapabilities arguments.

Here is an example that uses Sauce Labs (a company that provides Selenium remote webdriver servers as a service) to request an Internet Explorer 9 browser instance running on Windows 7.

代码语言:javascript复制
remote_server_url = ... # Specify the server URL

with Browser(driver_name="remote",
             url=remote_server_url,
             browser='internetexplorer',
             platform="Windows 7",
             version="9",
             name="Test of IE 9 on WINDOWS") as browser:
    print("Link to job: https://saucelabs.com/jobs/{}".format(
          browser.driver.session_id))
    browser.visit("http://splinter.readthedocs.org")
    browser.find_link_by_text('documentation').first.click()

3Cookies manipulation

It is possible to manipulate cookies using the cookies attribute from a Browser instance. The cookies attribute is a instance of a CookieManager class that manipulates cookies, like adding and deleting them.

Create cookie

To add a cookie use the add method: browser.cookies.add({'whatever': 'and ever'})

Retrieve all cookies

To retrieve all cookies use the all method: browser.cookies.all()

Delete a cookie

You can delete one or more cookies with the delete method:

代码语言:javascript复制
browser.cookies.delete('mwahahahaha')  # deletes the cookie 'mwahahahaha'

browser.cookies.delete('whatever', 'wherever')  # deletes two cookies

Delete all cookies

You can also delete all cookies: just call the delete method without any parameters:

代码语言:javascript复制
browser.cookies.delete()  # deletes all cookies

For more details check the API reference of the CookieManager class.

4 API docs

代码语言:javascript复制
class splinter.driver.webdriver.firefox.WebDriver(profile=None, extensions=None, user_agent=None, profile_preferences=None, fullscreen=False, wait_time=2, capabilities=None)
attach_file(name, value)

Fill the field identified by name with the content specified by value.

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:

代码语言:javascript复制
>>> 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 the choose method the following way:

代码语言:javascript复制
>>> browser.choose('gender', 'F')

Then you’re choosing the female gender.

clicklinkby_href(href)

Clicks in a link by its href attribute.

clicklinkbypartialhref(partial_href)

Clicks in a link by looking for partial content of href attribute.

clicklinkbypartialtext(partial_text)

Clicks in a link by partial content of its text.

clicklinkby_text(text)

Clicks in a link by its text.

cookies

A CookieManager instance.

For more details, check the cookies manipulation section(第三节).

evaluate_script(script)

Similar to execute_script method.

Executes javascript in the browser and returns the value of the expression.

代码语言:javascript复制
e.g.: ::
>>> assert 4 == browser.evaluate_script('2   2')
execute_script(script)

Executes a given JavaScript in the browser.

代码语言:javascript复制
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 by value.

fillform(fieldvalues)

Fill the fields identified by name with the content specified by value 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.

findbycss(css_selector)

Returns an instance of ElementList, using a CSS selector to query the current page content.

findbyid(id)

Finds an element in current page by its id.

Even when only one element is find, this method returns an instance of ElementList

findbyname(name)

Finds elements in current page by their name.

Returns an instance of ElementList.

findbytag(tag)

Find all elements of a given tag in current page.

Returns an instance of ElementList

findbytext(text)

Finds elements in current page by their text.

Returns an instance of ElementList

findbyvalue(value)

Finds elements in current page by their value.

Returns an instance of ElementList

findbyxpath(xpath, originalfind=None, originalquery=None)

Returns an instance of ElementList, using a xpath selector to query the urrent page content.

findlinkby_href(href)

Find all elements of a given tag in current page.

Returns an instance of ElementList

findlinkbypartialhref(partial_href)

Find links by looking for a partial str in their href attribute.

Returns an instance of ElementList

findlinkbypartialtext(partial_text)

Find links by looking for a partial str in their text.

Returns an instance of ElementList

findlinkby_text(text)

Find links querying for their text.

Returns an instance of ElementList

findoptionby_text(text)

Finds

Returns an instance of ElementList

findoptionby_value(value)

Finds

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(args, *kwds)

Changes the context for working with iframes.

For more details, check the docs about iframes, alerts and prompts

html

Source of current page.

iselementnotpresentbycss(cssselector, 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.

iselementnotpresentbyid(id, waittime=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.

iselementnotpresentbyname(name, waittime=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.

iselementnotpresentbytag(tag, waittime=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.

iselementnotpresentbytext(text, waittime=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.

iselementnotpresentbyvalue(value, waittime=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.

iselementnotpresentbyxpath(xpath, waittime=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.

iselementpresentbycss(cssselector, waittime=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.

iselementpresentbyid(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.

iselementpresentbyname(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.

iselementpresentbytag(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.

iselementpresentbytext(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.

iselementpresentbyvalue(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.

iselementpresentbyxpath(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.

istextpresent(text, wait_time=None)

Searchs for text in the browser and wait the seconds specified in wait_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=None, suffix='.png')

Takes a screenshot of the current page and saves it locally.

select(name, value)

Selects an element using the name of the

Example:

代码语言:javascript复制
>>> browser.select("state", "NY")
title

Title of current page.

type(name, value, slowly=False)

Types the value in the field identified by name.

It’s useful to test javascript events like keyPress, keyUp, keyDown, etc.

uncheck(name)

Unchecks a checkbox by its name.

Example:

代码语言:javascript复制
>>> 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

0 人点赞