Web Driver Methods in Selenium Python

19 May, 2020
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.
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 webdriverfrom selenium import webdriver   # create webdriver objectdriver = webdriver.Firefox()  
Arguments –Webdriver accepts various arguments to manipulate various features –
desired_capabilities – A dictionary of capabilities to request whenstarting 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 willbe started with given proxy settings, if possible. Optional.
keep_alive – Whether to configure remote_connection.RemoteConnection to useHTTP 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
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.
Let’s try to implement WebDriver methods using https://www.geeksforgeeks.org/ and play around with using javascript through selenium python.Program –
# import webdriverfrom selenium import webdriver  # create webdriver objectdriver = webdriver.Firefox()  # get geeksforgeeks.orgdriver.get("https://www.geeksforgeeks.org/")  # write scriptscript = "alert('Alert via selenium')"  # generate a alert via javascriptdriver.execute_async_script(script)
Output –Browser generates alert as verified below –
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.
Python-selenium
selenium
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python OOPs Concepts
How to iterate through Excel rows in Python?
Python Classes and Objects
Introduction To PYTHON
Decorators in Python
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
Rotate axis tick labels in Seaborn and Matplotlib
Queue in Python
Defaultdict in Python