Browser¶
To use Splinter, you must create a Browser instance:
from splinter import Browser
browser = Browser()
Alternatively, you can use a context manager using the with
statement:
from splinter import Browser
with Browser() as b:
# stuff using the browser
This will automatically close the browser before executing the code outside the with
statement.
Browser()
takes the following names, each mapped to a different driver:
For example:
browser = Browser('chrome')
Managing Windows¶
You can manage multiple windows (such as popups) through the windows object:
browser.windows # all open windows
browser.windows[0] # the first window
browser.windows[window_name] # the window_name window
browser.windows.current # the current window
browser.windows.current = browser.windows[3] # set current window to window 3
window = browser.windows[0]
window.is_current # boolean - whether window is current active window
window.is_current = True # set this window to be current window
window.next # the next window
window.prev # the previous window
window.close() # close this window
window.close_others() # close all windows except this one
This window management interface is not compatible with the undocumented interface exposed in v0.6.0 and earlier.
Verifying page content with Browser.html¶
You can use the html
attribute to get the html content of the visited page:
browser.html
Verifying page url with Browser.url¶
The visited page’s url can be accessed by the url
attribute:
browser.url
Changing Browser User-Agent¶
You can pass a User-Agent header on Browser instantiation.
b = Browser(user_agent="Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)")