Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. Selenium Tutorial covers all topics such as – WebDriver, WebElement, Unit Testing with selenium. This Python Selenium Tutorial covers Selenium from basics to advanced and professional uses.
Why learn Selenium Python ?
- Open Source and Portable – Selenium is an open source and portable Web testing Framework.
- Combination of tool and DSL – Selenium is combination of tools and DSL (Domain Specific Language) in order to carry out various types of tests.
- Easier to understand and implement – Selenium commands are categorized in terms of different classes which make it easier to understand and implement.
- Less burden and stress for testers – As mentioned above, the amount of time required to do testing repeated test scenarios on each and every new build is reduced to zero, almost. Hence, the burden of tester gets reduced.
- Cost reduction for the Business Clients – The Business needs to pay the testers their salary, which is saved using automation testing tool. The automation not only saves time but gets cost benefits too, to the business.
Selenium Python Basics
- Locating Single Elements –
- Locating Multiple Elements –
Advanced in Selenium Python –
Selenium Webdriver is the parent of all methods and classes used in Selenium Python. It is the driving force of Selenium that allows us to perform various operations on multiple elements on a webpage. Driver has various methods and attributes one can use to automate testing in Selenium Python. To check how to use webdriver, visit – Web Driver in Selenium Python
. Various methods one can use in selenium Python are –
||Adds a cookie to your current session.
||Goes one step backward in the browser history.
||Closes the current window.
||Creates a web element with the specified element_id.
||Delete all cookies in the scope of the session.
||Deletes a single cookie with the given name.
||Goes one step forward in the browser history.
||Invokes the window manager-specific ‘full screen’ operation
||Get a single cookie by name. Returns the cookie if found, None if not.
||Returns a set of dictionaries, corresponding to cookies visible in the current session.
||Gets the log for a given log type
||Gets the screenshot of the current window as a base64 encoded string which is useful in embedded images in HTML.
||Saves a screenshot of the current window to a PNG image file.
||Gets the screenshot of the current window as a binary data.
||Gets the x, y position of the current window.
||Gets the x, y coordinates of the window as well as height and width of the current window.
||Gets the width and height of the current window.
||Sets a sticky timeout to implicitly wait for an element to be found,
||Maximizes the current window that webdriver is using
||Invokes the window manager-specific ‘minimize’ operation
||Quits the driver and closes every associated window.
||Refreshes the current page.
||Set the amount of time to wait for a page load to complete before throwing an error.
||Set the amount of time that the script should wait during an execute_async_script call before throwing an error.
||Sets the x, y position of the current window. (window.moveTo)
||Sets the x, y coordinates of the window as well as height and width of the current window.
||Gets the URL of the current page.
||Returns the handle of the current window.
||Gets the source of the current page.
||Returns the title of the current page.
An element can be a tag, property, or anything, it is an instance of class
. After you find an element on screen using selenium, you might want to click it or find sub-elements, etc. Selenium provides methods around this WebElement of Selenium. To checkout how to use element object in selenium, visit – WebElement in Selenium Python
. Various methods one can use with an element in Selenium Python are discussed below –
||is_selected method is used to check if element is selected or not. It returns a boolean value True or False.
||is_displayed method is used to check if element it visible to user or not. It returns a boolean value True or False.
||is_enabled method is used to check if element is enabled or not. It returns a boolean value True or False.
||get_property method is used to get properties of an element, such as getting text_length property of anchor tag.
||get_attribute method is used to get attributes of an element, such as getting href attribute of anchor tag.
||send_keys method is used to send text to any field, such as input field of a form or even to anchor tag paragraph, etc.
||click method is used to click on any element, such as an anchor tag, a link, etc.
||clear method is used to clear text of any field, such as input field of a form or even to anchor tag paragraph, etc.
||screenshot method is used to save a screenshot of current element to a PNG file.
||submit method is used to submit a form after you have sent data to a form.
||value_of_css_property method is used to get value of a css property for a element.
||location method is used to get location of element in renderable canvas.
||screenshot_as_png method is used to gets the screenshot of the current element as binary data.
||parent method is used to get internal reference to the WebDriver instance this element was found from.
||size method is used to get size of current element.
||tag_name method is used to get name of tag you are referring to.
||text method is used to get text of current element.
||rect method is used to get a dictionary with the size and location of the element.
||screenshot_as_base64 method is used to gets the screenshot of the current element as a base64 encoded string.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!