Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Web Driver Methods in Selenium Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout – Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What we’d really like to do is to interact with the pages, or, more specifically, the HTML elements within a page. There are multiple strategies to find an element using Selenium, checkout – Locating Strategies. Selenium WebDriver offers various useful methods to control the session, or in other words, browser. For example, adding a cookie, pressing back button, navigating among tabs, etc.

This article revolves around Various WebDriver Methods and functions one can use to manipulate DOM and various other actions one can do with Selenium WebDriver in Python.

How to create an WebDriver Object ?

To create object of WebDriver, import WebDriver class from docs and create a object based on different Web Browser and Capabilities. After this one can use this object to perform all the operations of Webdriver. For example, to create object of Firefox, one can use –

# import webdriver
from selenium import webdriver
# create webdriver object
driver = webdriver.Firefox()

Arguments –
Webdriver accepts various arguments to manipulate various features –

  • desired_capabilities – A dictionary of capabilities to request when
    starting the browser session. Required parameter.
  • browser_profile – A selenium.webdriver.firefox.firefox_profile.FirefoxProfile object.
    Only used if Firefox is requested. Optional.
  • proxy – A selenium.webdriver.common.proxy.Proxy object. The browser session will
    be started with given proxy settings, if possible. Optional.
  • keep_alive – Whether to configure remote_connection.RemoteConnection to use
    HTTP keep-alive. Defaults to False.
  • file_detector – Pass custom file detector object during instantiation. If None,
    then default LocalFileDetector() will be used.
  • options – instance of a driver options.Options class

How to use Webdriver in Selenium ?

After one has created an object of Webdriver, open a webpage, and perform various other methods using below syntax and examples. one can perform various actions such as opening a new tab closing a tab, closing a window, adding a cookie, executing javascript, etc.

Project Example –

Let’s try to implement WebDriver methods using and play around with using javascript through selenium python.
Program –

# import webdriver
from selenium import webdriver
# create webdriver object
driver = webdriver.Firefox()
# get
# write script
script = "alert('Alert via selenium')"
# generate a alert via javascript

Output –
Browser generates alert as verified below –

WebDriver Methods in Selenium Python

One can perform a huge number of operations using Webdriver methods such as getting cookie, taking screenshot, etc. Here is a list of important methods used in webdriver.

add_cookieAdds a cookie to your current session.
backGoes one step backward in the browser history.
closeCloses the current window.
create_web_elementCreates a web element with the specified element_id.
delete_all_cookiesDelete all cookies in the scope of the session.

delete_cookieDeletes a single cookie with the given name.

execute_async_scriptAsynchronously Executes JavaScript in the current window/frame.

execute_scriptSynchronously Executes JavaScript in the current window/frame.

forwardGoes one step forward in the browser history.

fullscreen_windowInvokes the window manager-specific ‘full screen’ operation

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

get_cookiesReturns a set of dictionaries, corresponding to cookies visible in the current session.

get_logGets the log for a given log type

get_screenshot_as_base64Gets the screenshot of the current window as a base64 encoded string which is useful in embedded images in HTML.
get_screenshot_as_fileSaves a screenshot of the current window to a PNG image file.
get_screenshot_as_pngGets the screenshot of the current window as a binary data.

get_window_positionGets the x, y position of the current window.

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

get_window_sizeGets the width and height of the current window.

implicitly_waitSets a sticky timeout to implicitly wait for an element to be found,
maximize_windowMaximizes the current window that webdriver is using

minimize_windowInvokes the window manager-specific ‘minimize’ operation

quitQuits the driver and closes every associated window.

refreshRefreshes the current page.

set_page_load_timeoutSet the amount of time to wait for a page load to complete before throwing an error.
set_script_timeoutSet the amount of time that the script should wait during an execute_async_script call before throwing an error.
set_window_positionSets the x, y position of the current window. (window.moveTo)

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

current_urlGets the URL of the current page.

current_window_handleReturns the handle of the current window.

page_sourceGets the source of the current page.

titleReturns the title of the current page.

My Personal Notes arrow_drop_up
Last Updated : 19 May, 2020
Like Article
Save Article
Similar Reads
Related Tutorials