Thursday, December 26, 2024
Google search engine
HomeLanguagesWeb Driver Methods in Selenium Python

Web Driver Methods in Selenium Python

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 https://www.geeksforgeeks.org/ and play around with using javascript through selenium python.
Program –




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


Output –
Browser generates alert as verified below –
javascript-method-Selenium-Python

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.

Method Description
add_cookie Adds a cookie to your current session.
back Goes one step backward in the browser history.
close Closes the current window.
create_web_element Creates a web element with the specified element_id.
delete_all_cookies Delete all cookies in the scope of the session.
delete_cookie Deletes a single cookie with the given name.
execute_async_script Asynchronously Executes JavaScript in the current window/frame.
execute_script Synchronously Executes JavaScript in the current window/frame.
forward Goes one step forward in the browser history.
fullscreen_window Invokes the window manager-specific ‘full screen’ operation
get_cookie Get a single cookie by name. Returns the cookie if found, None if not.
get_cookies Returns a set of dictionaries, corresponding to cookies visible in the current session.
get_log Gets the log for a given log type
get_screenshot_as_base64 Gets the screenshot of the current window as a base64 encoded string which is useful in embedded images in HTML.
get_screenshot_as_file Saves a screenshot of the current window to a PNG image file.
get_screenshot_as_png Gets the screenshot of the current window as a binary data.
get_window_position Gets the x, y position of the current window.
get_window_rect Gets the x, y coordinates of the window as well as height and width of the current window.
get_window_size Gets the width and height of the current window.
implicitly_wait Sets a sticky timeout to implicitly wait for an element to be found,
maximize_window Maximizes the current window that webdriver is using
minimize_window Invokes the window manager-specific ‘minimize’ operation
quit Quits the driver and closes every associated window.
refresh Refreshes the current page.
set_page_load_timeout Set the amount of time to wait for a page load to complete before throwing an error.
set_script_timeout Set the amount of time that the script should wait during an execute_async_script call before throwing an error.
set_window_position Sets the x, y position of the current window. (window.moveTo)
set_window_rect Sets the x, y coordinates of the window as well as height and width of the current window.
current_url Gets the URL of the current page.
current_window_handle Returns the handle of the current window.
page_source Gets the source of the current page.
title Returns the title of the current page.
Last Updated :
19 May, 2020
Like Article
Save Article
Similar Reads
Related Tutorials
RELATED ARTICLES

Most Popular

Recent Comments