Open In App

Selenium Python Tutorial

Last Updated : 02 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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. Selenium-Python-Tutorial

Pre-requisite to Learn Selenium Python Tutorial

  • B asic Knowledge of Python: Before diving into Selenium, you should be comfortable with Python basics.
  • Understanding of Web Technologies: Familiarity with HTML, CSS, and JavaScript is crucial because Selenium interacts with webpages by manipulating these elements.
  • Basic Programming Concepts: Concepts like variables, data types, control structures (if-else, loops), functions, and error handling are essential.

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.

Learning Selenium with Python opens up many possibilities for efficient and effective web application testing, particularly when paired with popular cloud testing platforms like LambdaTest.

LambdaTest is an AI-powered test orchestration and execution platform that lets developers and testers perform Selenium Python testing at scale on a remote test lab of 3000+ real desktop browsers and operating systems. With Selenium Python, you can write robust test scripts to automate the testing of web applications, ensuring their functionality across different browsers and platforms. Developers and testers can even run tests in parallel on multiple combinations, helping them to ship quality builds at light speed.

Selenium Basics

Selenium Python Basics

Locating Strategies

Waits

Action Chains

Advanced in Selenium Python –

Project Examples

Selenium WebDriver

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 –

WebElement in Selenium Python

. Various methods one can use in selenium Python are –

MethodDescription
add_cookieAdds a cookie to your current session.
backGoes one step backward in the browser history.
closeCloses the current window.
create_web_elementCreates a web element with the specified element_id.
delete_all_cookiesDelete all cookies in the scope of the session.
delete_cookieDeletes a single cookie with the given name.
execute_async_scriptAsynchronously Executes JavaScript in the current window/frame.
execute_scriptSynchronously Executes JavaScript in the current window/frame.
forwardGoes one step forward in the browser history.
fullscreen_windowInvokes the window manager-specific ‘full screen’ operation
get_cookieGet a single cookie by name. Returns the cookie if found, None if not.
get_cookiesReturns a set of dictionaries, corresponding to cookies visible in the current session.
get_logGets the log for a given log type
get_screenshot_as_base64Gets the screenshot of the current window as a base64 encoded string which is useful in embedded images in HTML.
get_screenshot_as_fileSaves a screenshot of the current window to a PNG image file.
get_screenshot_as_pngGets the screenshot of the current window as a binary data.
get_window_positionGets the x, y position of the current window.
get_window_rectGets the x, y coordinates of the window as well as height and width of the current window.
get_window_sizeGets the width and height of the current window.
implicitly_waitSets a sticky timeout to implicitly wait for an element to be found,
maximize_windowMaximizes the current window that webdriver is using
minimize_windowInvokes the window manager-specific ‘minimize’ operation
quitQuits the driver and closes every associated window.
refreshRefreshes the current page.
set_page_load_timeoutSet the amount of time to wait for a page load to complete before throwing an error.
set_script_timeoutSet the amount of time that the script should wait during an execute_async_script call before throwing an error.
set_window_positionSets the x, y position of the current window. (window.moveTo)
set_window_rectSets the x, y coordinates of the window as well as height and width of the current window.
current_urlGets the URL of the current page.
current_window_handleReturns the handle of the current window.
page_sourceGets the source of the current page.
titleReturns the title of the current page.

Selenium WebElement

An element can be a tag, property, or anything, it is an instance of class

selenium.webdriver.remote.webelement.WebElement

. 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 –

. Various methods one can use with an element in Selenium Python are discussed below –

Element MethodsDescription
is_selected()is_selected method is used to check if element is selected or not. It returns a boolean value True or False.
is_displayed()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()is_enabled method is used to check if element is enabled or not. It returns a boolean value True or False.
get_property()get_property method is used to get properties of an element, such as getting text_length property of anchor tag.
get_attribute()get_attribute method is used to get attributes of an element, such as getting href attribute of anchor tag.
send_keys()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()click method is used to click on any element, such as an anchor tag, a link, etc.
clear()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()screenshot method is used to save a screenshot of current element to a PNG file.
submit()submit method is used to submit a form after you have sent data to a form.
value_of_css_property()value_of_css_property method is used to get value of a css property for a element.
locationlocation method is used to get location of element in renderable canvas.
screenshot_as_pngscreenshot_as_png method is used to gets the screenshot of the current element as binary data.
parentparent method is used to get internal reference to the WebDriver instance this element was found from.
sizesize method is used to get size of current element.
tag_nametag_name method is used to get name of tag you are referring to.
texttext method is used to get text of current element.
rectrect method is used to get a dictionary with the size and location of the element.
screenshot_as_base64screenshot_as_base64 method is used to gets the screenshot of the current element as a base64 encoded string.


Similar Reads