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 –
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 ?
Project Example –
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_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.|
|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.|
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course