Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Webscraping
  • Beautiful Soup
  • Request
  • Scrapy
  • open cv
  • Data analysis
  • Machine learning
  • NLP
  • Deep learning
  • Data Science
  • Interview question
  • ML math
  • ML Projects
  • ML interview
  • DL interview
Open In App
Next Article:
back driver method - Selenium Python
Next article icon

Web Driver Methods in Selenium Python

Last Updated : 19 May, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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
driver.get("https://www.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.


Next Article
back driver method - Selenium Python

N

NaveenArora
Improve
Article Tags :
  • Python
  • Selenium
  • Software Testing
  • Python-selenium
Practice Tags :
  • python

Similar Reads

  • title driver method - 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 bein
    2 min read
  • create_web_element driver method - 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 bein
    2 min read
  • quit driver method - 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 bein
    2 min read
  • back driver method - 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 bein
    2 min read
  • forward driver method - 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 bein
    2 min read
  • close driver method - 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 bein
    2 min read
  • find_element() driver method - 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. After you have installed selenium and checked out - Navigating links using get method , you might want to play mor
    3 min read
  • get_log driver method - 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 bein
    2 min read
  • maximize_window driver method - 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 bein
    2 min read
  • implicitly_wait driver method - 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 bein
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences