4. Library Documentation

All features of the library can be accessed using the object of the class Browser( ) . All examples below assume the object name as “driver”

Interface:
from webbot import Browser
driver = Browser()

4.1. Module contents

class webbot.Browser(showWindow=True, proxy=None, downloadPath: str = None)

Bases: object

Constructor

__init__(showWindow = True , proxy = None):
 

The constructor takes showWindow flag as argument which Defaults to False. If it is set to true , all browser happen without showing up any GUI window .

Args:
  • showWindow : If true , will run a headless browser without showing GUI window.
  • proxy : Url of any optional proxy server.

Object attributes: Key , errors

Key:
  • It contains the constants for all the special keys in the keyboard which can be used in the press method
errors:
  • List containing all the errors which might have occurred during performing an action like click ,type etc.
click(text='', tag='button', id='', classname='', number=1, css_selector='', xpath='', loose_match=True, multiple=False)

Clicks one or more elements on the webpage.

Args:
  • text: The text of the element that needs to be clicked.
  • tag: The html tag of the element to be clicked (eg: button, a), defaults to ‘button’.
  • id: id of the element.
  • classname: Any class of the element to consider while selecting the element to click.
  • number: If there are multiple elements matching the criteria of other parameters, number specifies which element to select for the operation. This defaults to 1 and selects the first element to perform the action.
  • multiple: Defaults to False. If True, the specified action is performed on all the elements matching the criteria and not just the first element. If it is true, number parameter is ignored.
  • css_selector: css_selector expression for better control over selecting the elements to perform the action.
  • xpath: xpath expression for better control over selecting the elements to perform the action.
  • loose_match: Defaults to True. If loose_match is True then if no element of specified tag is found, all other tags are considered to search for the text, else only specified tag is considered for matching elements.
Usage:
driver = Browser()
driver.go_to('google.com')

driver.click('Sign In')
driver.click('Sign In', tag='span' )
driver.click(id = 'elementid')

# if there are multiple elements matching the text "NEXT",
 then 2'nd element is clicked (since number parameter is 2 ).
driver.click("NEXT" , tag='span' , number = 2 )
close_current_tab()

Closes the current tab which the driver is controlling

exists(text='', tag='button', id='', classname='', number=1, css_selector='', xpath='', loose_match=True)

Check if an element exists or not.

Returns True if any element that best fits the given parameters exists. Return False if no such element exists.

Args:
  • text : The text of the element.
  • tag : The html tag of the element to look for (eg : button , a ) , defaults to ‘button’.
  • id : id of the element.
  • classname : Any class of the element to search for.
  • number : if there are multiple elements matching the criteria of other parameters, number specifies which element to select for the operation. This defaults to 1 and selects the first element to perform the action.
  • multiple : Defaults to False. If True, the specified action is performed on all the elements matching the criteria and not just the first element. If it is true, number parameter is ignored.
  • css_selector : css_selector expression for better control over selecting the elements to perform the action.
  • xpath : xpath expression for better control over selecting the elements to perform the action.
  • loose_match : Defaults to True. If loose_match is True then if no element of specified tag is found, all other tags are considered to search for the text, else only specified tag is considered for matching elements.

:Usage :

driver = Browser()
driver.go_to('google.com')

driver.exists('Sign In') ;  #Returns True
driver.exists('yahoo') ;  #Returns False
find_elements(text='', tag='button', id='', number=1, classname='', css_selector='', xpath='', loose_match=True)

Returns a list of elements that best fit the given parameters

get_application_cache()

Get application cache object to interact with the browser app cache

get_current_url()

Get the current url of the webpage

get_current_window_handle()

get the window handle of the current window or tab which the web driver is controlling

get_desired_capabilities()

returns the drivers current desired capabilities being used

get_log_types()

Get supported log types to be used by the get_log method

get_page_source()

Gets the html source code for the current webpage

get_title()

Gets the title of the current webpage

get_total_tabs()

Gets the total number of tabs or windows that is currently open.

go_back()

Go back to the previous URL. It’s same as clicking the back button in browser.

go_forward()

It’s same as clicking the forward button in the browser

go_to(url)

Open the webpage corresponding to the url given in the parameter. If the url doesn’t contain the protocol of the url, then by default https is considered.

new_tab(url='https://google.com')

Opens a new tab.

press(key)

Press any special key or a key combination involving Ctrl , Alt , Shift

Args:-key: A key present in Browser().Key added with any other key to get the key combination.
Usage:
press(driver.Key.SHIFT + 'hello')  # Sends keys HELLO in capital letters

press(driver.Key.CONTROL + driver.Key.UP )

press(driver.Key.ENTER)
scrollx(amount)

Scroll horizontally by the specified amount

Args:
  • amount: positive integer for scrolling right or negative integer for scrolling left
Usage:
scrollx(100)
scrollx(-200)
scrolly(amount)

Scroll vertically by the specified amount

Args:
  • amount: positive integer for scrolling down or negative integer for scrolling up
Usage:
scrolly(100)
scrolly(-200)
switch_to_tab(number)
Switch to the tab corresponding to the number argument.
The tabs are numbered in the order that they are opened by the web driver.

So changing the order of the tabs in the browser won’t change the tab numbers.

type(text, into='', clear=True, multiple=False, tag='input', id='', classname='', number=1, css_selector='', xpath='', loose_match=True)

Types the text into an input field

Args:
  • text: The text to type in the input field.
  • into: This can be any placeholder or name or value that is seen inside the input text box as seen in a browser.

If not specified, other params are considered or the first input field is selected.

  • clear: Defaults to True. Clears the input field before typing the text.
  • tag: defaults to ‘input’. The html tag to consider for the input field (eg: textarea).
  • id: id of the element to which the text must be sent.
  • classname: Any class of the input element to consider while selecting the input element to send the keys to.
  • number: If there are multiple elements matching the criteria of other parameters, number specifies which element to select for the operation. This defaults to 1 and selects the first element to perform the action.
  • multiple: Defaults to False.

If True, the specified action is performed on all the elements matching the criteria and not just the first element.

  • css_selector: css_selector expression for better control over selecting the elements to perform the action.
  • xpath: xpath expression for better control over selecting the elements to perform the action.
  • loose_match: Defaults to True.

If loose_match is True then if no element of specified tag is found, all other tags are considered to search for the text, else only specified tag is considered for matching elements.

Usage:
driver = Browser()
driver.go_to('mail.google.com')

driver.type('Myemail@gmail.com', into = 'Email')
driver.type('mysecretpassword', into = 'Password' , id = 'passwordfieldID' )
driver.type("hello", tag='span', number = 2 )   '''if there are multiple elements,
then 2nd one is considered for operation (since number parameter is 2 )'''.

Methods included from Selenium:
 

All these methods can be accessed using the object of the class Browser (i.e driver object )

Adds a cookie to your current session.

Args:
  • cookie_dict: A dictionary object, with required keys - “name” and “value”;
    optional keys - “path”, “domain”, “secure”, “expiry”
Usage:
driver.add_cookie({‘name’ : ‘foo’, ‘value’ : ‘bar’}) driver.add_cookie({‘name’ : ‘foo’, ‘value’ : ‘bar’, ‘path’ : ‘/’}) driver.add_cookie({‘name’ : ‘foo’, ‘value’ : ‘bar’, ‘path’ : ‘/’, ‘secure’:True})

Deletes a single cookie with the given name.

Usage:driver.delete_cookie(‘my_cookie’)
Chrome.delete_all_cookies()

Delete all cookies in the scope of the session.

Usage:driver.delete_all_cookies()
Chrome.execute_script(script, *args)

Synchronously Executes JavaScript in the current window/frame.

Args:
  • script: The JavaScript to execute.
  • *args: Any applicable arguments for your JavaScript.
Usage:

driver.execute_script(‘return document.title;’)

Chrome.execute_async_script(script, *args)

Asynchronously Executes JavaScript in the current window/frame.

Args:
  • script: The JavaScript to execute.
  • *args: Any applicable arguments for your JavaScript.
Usage:

script = “var callback = arguments[arguments.length - 1]; ” “window.setTimeout(function(){ callback(‘timeout’) }, 3000);” driver.execute_async_script(script)

Chrome.fullscreen_window()

Invokes the window manager-specific ‘full screen’ operation

Get a single cookie by name. Returns the cookie if found, None if not.

Usage:driver.get_cookie(‘my_cookie’)
Chrome.get_log(log_type)

Gets the log for a given log type

Args:
  • log_type: type of log that which will be returned
Usage:

driver.get_log(‘browser’) driver.get_log(‘driver’) driver.get_log(‘client’) driver.get_log(‘server’)

Chrome.get_network_conditions()

Gets Chrome network emulation settings.

Returns:

A dict. For example:

{‘latency’: 4, ‘download_throughput’: 2, ‘upload_throughput’: 2, ‘offline’: False}

Chrome.get_screenshot_as_base64()
Gets the screenshot of the current window as a base64 encoded string
which is useful in embedded images in HTML.
Usage:driver.get_screenshot_as_base64()
Chrome.get_screenshot_as_file(filename)
Saves a screenshot of the current window to a PNG image file. Returns
False if there is any IOError, else returns True. Use full paths in your filename.
Args:
  • filename: The full path you wish to save your screenshot to. This should end with a .png extension.
Usage:

driver.get_screenshot_as_file(‘/Screenshots/foo.png’)

Chrome.get_screenshot_as_png()

Gets the screenshot of the current window as a binary data.

Usage:driver.get_screenshot_as_png()
Chrome.get_window_position(windowHandle='current')

Gets the x,y position of the current window.

Usage:driver.get_window_position()
Chrome.get_window_rect()

Gets the x, y coordinates of the window as well as height and width of the current window.

Usage:driver.get_window_rect()
Chrome.get_window_size(windowHandle='current')

Gets the width and height of the current window.

Usage:driver.get_window_size()
Chrome.maximize_window()

Maximizes the current window that webdriver is using

Chrome.minimize_window()

Invokes the window manager-specific ‘minimize’ operation

Chrome.implicitly_wait(time_to_wait)
Sets a sticky timeout to implicitly wait for an element to be found,
or a command to complete. This method only needs to be called one time per session. To set the timeout for calls to execute_async_script, see set_script_timeout.
Args:
  • time_to_wait: Amount of time to wait (in seconds)
Usage:

driver.implicitly_wait(30)

Chrome.quit()

Closes the browser and shuts down the ChromeDriver executable that is started when starting the ChromeDriver

Chrome.refresh()

Refreshes the current page.

Usage:driver.refresh()
Chrome.save_screenshot(filename)
Saves a screenshot of the current window to a PNG image file. Returns
False if there is any IOError, else returns True. Use full paths in your filename.
Args:
  • filename: The full path you wish to save your screenshot to. This should end with a .png extension.
Usage:

driver.save_screenshot(‘/Screenshots/foo.png’)

Chrome.set_network_conditions(**network_conditions)

Sets Chrome network emulation settings.

Args:
  • network_conditions: A dict with conditions specification.
Usage:
driver.set_network_conditions(

offline=False, latency=5, # additional latency (ms) download_throughput=500 * 1024, # maximal throughput upload_throughput=500 * 1024) # maximal throughput

Note: ‘throughput’ can be used to set both (for download and upload).

Chrome.set_page_load_timeout(time_to_wait)
Set the amount of time to wait for a page load to complete
before throwing an error.
Args:
  • time_to_wait: The amount of time to wait
Usage:

driver.set_page_load_timeout(30)

Chrome.set_window_position(x, y, windowHandle='current')

Sets the x,y position of the current window. (window.moveTo)

Args:
  • x: the x-coordinate in pixels to set the window position
  • y: the y-coordinate in pixels to set the window position
Usage:

driver.set_window_position(0,0)

Chrome.set_window_rect(x=None, y=None, width=None, height=None)

Sets the x, y coordinates of the window as well as height and width of the current window.

Usage:driver.set_window_rect(x=10, y=10) driver.set_window_rect(width=100, height=200) driver.set_window_rect(x=10, y=10, width=100, height=200)
Chrome.start_client()

Called before starting a new session. This method may be overridden to define custom startup behavior.

Chrome.start_session(capabilities, browser_profile=None)

Creates a new session with the desired capabilities.

Args:
  • browser_name - The name of the browser to request.
  • version - Which browser version to request.
  • platform - Which platform to request the browser on.
  • javascript_enabled - Whether the new session should support JavaScript.
  • browser_profile - A selenium.webdriver.firefox.firefox_profile.FirefoxProfile object. Only used if Firefox is requested.
Chrome.stop_client()

Called after executing a quit command. This method may be overridden to define custom shutdown behavior.

Chrome.switch_to_alert()

Deprecated use driver.switch_to.alert