<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: EugeneKwaka</title>
    <description>The latest articles on Forem by EugeneKwaka (@eugenekwaka_24).</description>
    <link>https://forem.com/eugenekwaka_24</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F981905%2F601e951b-59ac-4d96-bd3d-8d11484d42f1.png</url>
      <title>Forem: EugeneKwaka</title>
      <link>https://forem.com/eugenekwaka_24</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/eugenekwaka_24"/>
    <language>en</language>
    <item>
      <title>How To Handle WebElements In Selenium Python</title>
      <dc:creator>EugeneKwaka</dc:creator>
      <pubDate>Wed, 12 Apr 2023 05:30:41 +0000</pubDate>
      <link>https://forem.com/testmuai/how-to-handle-webelements-in-selenium-python-51jl</link>
      <guid>https://forem.com/testmuai/how-to-handle-webelements-in-selenium-python-51jl</guid>
      <description>&lt;p&gt;Selenium automation testing is the common term used to describe a test run using Selenium. When it comes to automation testing, &lt;a href="https://www.lambdatest.com/selenium?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; is a popular open-source testing tool used to automate different web browsers, such as Chrome, Firefox, Microsoft Edge, and Safari, among others, across multiple platforms. The S&lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;elenium WebDriver&lt;/a&gt; allows you to execute test scripts in various programming languages such as Java, Python, C#, JavaScript, PHP, and Ruby.&lt;/p&gt;

&lt;p&gt;In Selenium, a WebElement represents an HTML element of a web page. WebElements are the building blocks of the web page. For example, anything on a web page, like a text box, button, links, dropdown buttons, etc., comes under WebElements.&lt;/p&gt;

&lt;p&gt;Selenium WebDriver takes every single element and encapsulates it as an object of the WebElement. It provides an API to locate the web page elements and take a specific action like entering text into text boxes, clicking the buttons, etc. Using the Selenium framework, you can easily locate and interact with WebElements while performing &lt;a href="https://www.lambdatest.com/selenium-python-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Python testing&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Handling WebElements is important when automating &lt;a href="https://www.lambdatest.com/web-application-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;web application testing&lt;/a&gt;. In this blog, we will look at how to handle WebElements in Selenium Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Automate&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/cypress-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;Cypress testing&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;and perform browser automation testing with LambdaTest. Our cloud infrastructure has 3000+ desktop &amp;amp; mobile environments. Try for free.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What are WebElements in Selenium?
&lt;/h3&gt;

&lt;p&gt;WebElements in Selenium are HTML elements that make up the web page’s content. Each HTML document consists of several HTML elements. An HTML element comprises a start tag and an end tag determining its position in the document. Between those HTML tags lies the content of the document.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
content comes here 
&amp;amp;lt;/end tag&amp;amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The WebElement interface in Selenium allows you to interact with both visible and invisible elements on a web page. In Selenium, all the methods return a value or no value (null/void). Similarly, the WebElement command returns either null/void or the element being searched for.&lt;/p&gt;

&lt;p&gt;Shown below is the command of a WebElement in Selenium:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; WebElement element = driver.findElement(By.id(“UserName“));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/VeV_sup5S8E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Subscribe to the &lt;a href="https://www.youtube.com/c/LambdaTest?sub_confirmation=1?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=youtube" rel="noopener noreferrer"&gt;LambdaTest YouTube Channel&lt;/a&gt; and get the latest tutorials around &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt;, Selenium testing, &lt;a href="https://www.lambdatest.com/playwright?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Playwright&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/appium-mobile-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Appium&lt;/a&gt;, and more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling WebElements in Selenium Python
&lt;/h3&gt;

&lt;p&gt;Selenium makes it simple for developers to automate &lt;a href="https://www.lambdatest.com/learning-hub/web-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;web testing&lt;/a&gt; and help you handle WebElements in Selenium Python when performing &lt;a href="https://www.lambdatest.com/python-automation-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Python automation testing&lt;/a&gt;. Here are some of the best approaches to handling WebElements in Selenium Python.&lt;/p&gt;

&lt;h3&gt;
  
  
  Alert Pop-Ups
&lt;/h3&gt;

&lt;p&gt;Alerts and pop-ups are notification messages that alert a user about information or request authorization to carry out certain tasks. The appearance of an alert while performing &lt;a href="https://www.lambdatest.com/python-web-automation?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Python web automation&lt;/a&gt; hinders the Selenium WebDriver from interacting with the webpage’s elements until the alert is dismissed. This leads to tests stalling or failing if the alerts or pop-ups are not handled.&lt;/p&gt;

&lt;p&gt;When an alert is prompted, the alert pop-up window is displayed on the current webpage. Since the Selenium WebDriver’s context is on the current page, we need to switch the WebDriver’s focus to the new alert window.&lt;/p&gt;

&lt;p&gt;This is done using the &lt;strong&gt;switch_to.alert()&lt;/strong&gt; code command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alert_object = driver.switch_to.alert
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The other alert handling methods include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;accept()&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alert_object = Alert(driver)
alert_object.accept()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;dismiss()&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;dismiss()&lt;/strong&gt; method cancels the alert prompt.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alert_object = Alert(driver)
alert_object.dismiss()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;send_keys()&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;send_keys()&lt;/strong&gt; method accepts an argument, which will be entered in the alert pop-up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alert_object = Alert(driver)
alert_object.send_keys("Okay")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;text()&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;text()&lt;/strong&gt; method retrieves and displays the text that is in the alert pop-up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alert_object = Alert(driver)
print(alert_object.text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Demonstration: How to handle an Alert in Selenium Python
&lt;/h4&gt;

&lt;p&gt;Let us look at an example of a prompt alert and how we can handle it in &lt;a href="https://www.lambdatest.com/learning-hub/python-tutorial?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium Python&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To demonstrate how to handle an alert, we will use the example of the JavaScript Alert Box Demo from the &lt;a href="https://www.lambdatest.com/selenium-playground/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Selenium Playground&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to the &lt;a href="https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;JavaScript Alert Box&lt;/a&gt; demo page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the &lt;strong&gt;Click Me&lt;/strong&gt; button in the last JavaScript Alert Box page&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter your name on the alert pop-up.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the &lt;strong&gt;OK&lt;/strong&gt; button.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Test Case on the Chrome Browser):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let us write a test script to handle a simple alert.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options as ChromeOptions


def test_handling_simple_alert():
    options = ChromeOptions()
    options.browser_version = "108.0"
    options.platform_name = "Windows 10"
    lt_options = {};
    lt_options["username"] = os.environ.get('LT_USERNAME');
    lt_options["accessKey"] = os.environ.get('LT_ACCESS_KEY');
    lt_options["build"] = "Handling Alerts";
    lt_options["project"] = "Handling Alerts";
    lt_options["name"] = "Handling Alerts";
    lt_options["w3c"] = True;
    lt_options["plugin"] = "python-python";
    options.set_capability('LT:Options', lt_options);

    # LambdaTest Profile username
    user_name = os.environ.get('LT_USERNAME')
    # LambdaTest Profile access_key
    accesskey = os.environ.get('LT_ACCESS_KEY')
    remote_url =  "https://" + user_name + ":" + accesskey + "@hub.lambdatest.com/wd/hub"
    driver = webdriver.Remote(remote_url, options=options)

    driver.get("https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo")
    # Locate the button that prompts the alert
    alert_button = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/section[4]/div/div/div[2]/div[3]/p[3]/button')))
    alert_button.click()
    # Switch the webdriver's control to the alert pop-up
    alert_object = driver.switch_to.alert
    # Show the alert message
    print("This is the alert message: " + alert_object.text)
    #Enter text into the Alert using send_keys()
    alert_object.send_keys('Eugene')
    print("Entered my name in the prompt box")
    # Use the alert.accept() method to accept the alert
    alert_object.accept()
    print("Alert pop-up accepted by clicking the 'OK' button")

    # End the driver session
    driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;This&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/playwright-framework?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;Playwright browser testing&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;tutorial will guide you through the setup of the Playwright framework, which will enable you to write end-to-end tests for your future projects.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We then run the test script by running the following command in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m  pytest test_handling_alerts.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the test is executed, the following output is displayed to show that the Selenium automated test was successful.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A5eLvzCHDPHJ4an04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A5eLvzCHDPHJ4an04.png" alt="image" width="711" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can view the test logs in the build section from the &lt;a href="https://automation.lambdatest.com/build?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Automation Dashboard&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AGxAe_SrK91A0oWC7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AGxAe_SrK91A0oWC7.png" alt="image" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let us review the code for the test script we wrote in the section above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Import the Selenium libraries needed to run the test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AacohdNGM4H9iCTyC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AacohdNGM4H9iCTyC.png" alt="image" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, we import &lt;strong&gt;pytest&lt;/strong&gt;, a testing framework for Python that enables us to write unit tests. The &lt;strong&gt;By&lt;/strong&gt; query helps us locate web elements within the &lt;a href="https://www.lambdatest.com/blog/document-object-model/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Document Object Model&lt;/a&gt; (DOM) using the &lt;a href="https://www.lambdatest.com/blog/findelement-and-findelements-in-selenium/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;find_element() method in Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;WebDriverWait&lt;/strong&gt; class combined with &lt;strong&gt;expected_conditions&lt;/strong&gt; allows us to specify the period of time the script should wait for a given condition to occur before continuing with the code. Finally, the &lt;strong&gt;Options (ChromeOptions)&lt;/strong&gt; class customizes the ChromeDriver sessions and helps us perform operations like opening the Chrome browser.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Generate the options using the &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Capabilities Generator&lt;/a&gt; to run the test in the Chrome browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ArxqfUV6xlzzycKkl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ArxqfUV6xlzzycKkl.png" alt="image" width="800" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Create a &lt;strong&gt;remote_url&lt;/strong&gt; variable that connects to the Remote Selenium Grid &lt;strong&gt;(@&lt;/strong&gt;&lt;a href="http://hub.lambdatest.com/wd/hub" rel="noopener noreferrer"&gt;&lt;strong&gt;hub.lambdatest.com/wd/hub&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;)&lt;/strong&gt; using the username and accesskey listed on the &lt;a href="https://accounts.lambdatest.com/detail/profile" rel="noopener noreferrer"&gt;LambdaTest Profile&lt;/a&gt; page. We then pass the options and the &lt;strong&gt;remote_url&lt;/strong&gt; to instantiate the driver constructor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Ax5SwjDrFYfLvtYdY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Ax5SwjDrFYfLvtYdY.png" alt="image" width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Open the LambdaTest Playground website and locate the button that prompts the alert using its &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;XPath Selector&lt;/a&gt; in the browser inspect tools, then click it to initiate the alert.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A1ziCqfwJUayoXZjH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A1ziCqfwJUayoXZjH.png" alt="image" width="800" height="136"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AS3kW_USLRu98FlLj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AS3kW_USLRu98FlLj.png" alt="image" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the &lt;strong&gt;Click Me&lt;/strong&gt; button is clicked, the below window will be shown with the prompt alert.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Ax5zh02uaPuMNCcXI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Ax5zh02uaPuMNCcXI.png" alt="image" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The WebDriver’s focus is then switched to the alert prompt using the &lt;strong&gt;switch_to.alert&lt;/strong&gt; method. Since the alert requires an input, we use the &lt;a href="https://www.lambdatest.com/blog/how-to-use-selenium-sendkeys/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;send_keys() method in Selenium&lt;/a&gt; to pass a value (in this case, your name), and then we accept the alert pop-up using the &lt;strong&gt;accept()&lt;/strong&gt; method. The WebDriver session is then closed using the &lt;strong&gt;driver.quit()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AhfTC0MX1OH4WvWGK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AhfTC0MX1OH4WvWGK.png" alt="image" width="800" height="523"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Drag and Drop Operations
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://www.lambdatest.com/blog/drag-and-drop-in-selenium-python/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;drag and drop operation in Selenium Python&lt;/a&gt; allows users to use a mouse to move (drag) web elements from one location to another. In test automation using Selenium Python, the drag and drop function can be difficult to automate since it necessitates several actions and interactions.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;ActionChains&lt;/strong&gt; class in Selenium Python provides methods to simulate the mouse events required for drag-and-drop. The methods are typically used to automate actions like mouse movements, keystrokes, context menu clicks, and mouse button events. The ActionChains object implements the class as a queue and calls the &lt;strong&gt;perform()&lt;/strong&gt; method, which executes the actions together to prevent the test failure.&lt;/p&gt;

&lt;p&gt;The syntax for creating an ActionChains object is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium.webdriver import ActionChains
action = ActionChains(driver)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;drag_and_drop(source, target)&lt;/strong&gt; method performs the action of holding the left mouse button on the source element and releasing the mouse button at the target element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;action.drag_and_drop(source, target).perform()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Demonstration: How to perform a Drag-and-Drop Operation in Selenium Python
&lt;/h4&gt;

&lt;p&gt;Let us look at how we can handle a drag-and-drop operation in Selenium using Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To implement drag and drop, we will use the example of Drag Drop Range Sliders from the LambdaTest Selenium Playground.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the &lt;a href="https://www.lambdatest.com/selenium-playground/drag-drop-range-sliders-demo?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Drag Drop Range Sliders&lt;/a&gt; demo page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Locate the drag pointer and drag it from 5 (source) to 50 (target)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Test Case on the Chrome Browser):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver import ActionChains


def test_handling_drag_and_drop():
    options = ChromeOptions()
    options.browser_version = "106.0"
    options.platform_name = "Windows 10"
    lt_options = {};
    lt_options["username"] = os.environ.get("LT_USERNAME");
    lt_options["accessKey"] = os.environ.get("LT_ACCESS_KEY");
    lt_options["visual"] = True;
    lt_options["build"] = "Handling Drag &amp;amp; Drop";
    lt_options["project"] = "Handling Drag &amp;amp; Drop";
    lt_options["name"] = "Handling Drag &amp;amp; Drop";
    lt_options["w3c"] = True;
    lt_options["plugin"] = "python-python";
    options.set_capability('LT:Options', lt_options);
    # LambdaTest Profile username
    user_name = os.environ.get('LT_USERNAME')
    # LambdaTest Profile access_key
    accesskey = os.environ.get('LT_ACCESS_KEY')
    remote_url =  "https://" + user_name + ":" + accesskey + "@hub.lambdatest.com/wd/hub"
    driver = webdriver.Remote(remote_url, options=options)
    driver.get("https://www.lambdatest.com/selenium-playground/drag-drop-range-sliders-demo")

    source = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[value='5']")))
    print("I am dragging the mouse from: ", source.get_attribute("value"))
    target = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[value='50']")))
    print("I'm dropping the mouse at: ", target.get_attribute("value"))
    driver.implicitly_wait(10)
    action = ActionChains(driver)
    action.drag_and_drop(source, target).perform()
    print("The mouse dragged from 5 to 50")
    driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Pass the following command in the command line to execute the test script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pytest test_handling_drag_and_drop.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test passes once it runs and displays the following output in the terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AxkwHKEVtoIAKRsO8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AxkwHKEVtoIAKRsO8.png" alt="image" width="800" height="92"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the LambdaTest Automation Dashboard, you can see the results of the &lt;a href="https://www.lambdatest.com/learning-hub/test-execution?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test execution&lt;/a&gt;. You can also see the live recordings of the test with detailed reports and logs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Abl9FxaX5z1Jh6UGk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Abl9FxaX5z1Jh6UGk.png" alt="image" width="800" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you need to gather more knowledge on&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/website-testing/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;testing web&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;? LambdaTest is here to explain further.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Import the Selenium libraries essential to the test script, including the &lt;strong&gt;ActionChains&lt;/strong&gt; class that replicates mouse events for our test automation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A1cMCsr34TCBf9fPn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A1cMCsr34TCBf9fPn.png" alt="image" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Using the &lt;strong&gt;driver.get(url)&lt;/strong&gt; method, we navigate to the URL where our automated test will simulate the drag and drop operation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A9jPLcc1l-8U4ZyLQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A9jPLcc1l-8U4ZyLQ.png" alt="image" width="800" height="159"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Next, locate the source web element and the target elements the mouse will drag and drop onto. We use the elements’ &lt;a href="https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;CSS Selectors&lt;/a&gt; to locate them from the web browser’s inspect tools. The source element has an input value of 5, which will be moved to the target element, which has an input value of 50.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ArlDII-fcTVe5CwsG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ArlDII-fcTVe5CwsG.png" alt="image" width="800" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Locating the source element using the CSS Selector in the Browser Inspect Tools:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AIV37erFuKf4J4WR8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AIV37erFuKf4J4WR8.png" alt="image" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Locating the target element using the CSS Selector in Browser Inspect Tools:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AAtAnbWGD8yxymPZX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AAtAnbWGD8yxymPZX.png" alt="image" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; You then create an object of ActionChains and &lt;strong&gt;invoke drag_and_drop(source, target).perform()&lt;/strong&gt; method to execute the mouse movement from 5 to 50 in the draggable slide.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; We finally close the webdriver session using the &lt;strong&gt;driver.close()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A0JcAbxUQnCSe4OJf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A0JcAbxUQnCSe4OJf.png" alt="image" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Selecting Multiple Values in Drop-Down Menus
&lt;/h3&gt;

&lt;p&gt;A drop-down is a navigation element of a website that can be used in forms, search filter features and the Navbar. It allows the selection of one or more options from a list of options.&lt;/p&gt;

&lt;p&gt;Choosing multiple values from a drop-down menu while running automated tests in Selenium Python can be a challenge for testers. Automating the selection of multiple choices is complex, as the test script needs to identify the drop-down element with its options and then search through the list of options before selecting them.&lt;/p&gt;

&lt;p&gt;In some instances, the drop-down menu options are dynamically generated and rendered differently based on the web browser or operating system used. This can make writing robust and efficient scripts challenging.&lt;/p&gt;

&lt;p&gt;Below are screenshots of the same drop-down menu in Chrome and Firefox browsers on a Windows OS.&lt;/p&gt;

&lt;p&gt;Drop-down menu in a Chrome browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AZZ5OEC5RQCvuM1K5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AZZ5OEC5RQCvuM1K5.png" alt="image" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Drop-down menu in a Firefox browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AmAiVgdd_4KRJ8aWr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AmAiVgdd_4KRJ8aWr.png" alt="image" width="800" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The drop-down menu background color and font type are different in both browsers.&lt;/p&gt;

&lt;p&gt;We use the &lt;strong&gt;Select class&lt;/strong&gt; when testing with Selenium Python to address this issue. The Selenium WebDriver provides the Select class to handle single-select and multiple-select drop-downs with the &lt;code&gt;&amp;lt; select &amp;gt;&lt;/code&gt; HTML tag. In Selenium Python, there are several methods for selecting dropdowns.&lt;/p&gt;

&lt;p&gt;We will implement the following three methods to select multiple values in our test scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;select_by_index(index)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This method uses the &lt;strong&gt;get_attribute(“index”)&lt;/strong&gt; of a &lt;code&gt;&amp;lt; option &amp;gt;&lt;/code&gt; tag to determine its index and return the matching option.This method uses the &lt;strong&gt;get_attribute(“index”)&lt;/strong&gt; of a &lt;code&gt;&amp;lt; option &amp;gt;&lt;/code&gt; tag to determine its index and return the matching option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; multi_select = Select(dropdown)
 multi_select.select_by_index(0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;select_by_visible_text(text)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This method selects the options that display text that matches the argument passed. For example, the argument “Iowa” will select an option tag with &lt;code&gt;&amp;lt; option &amp;gt; Iowa &amp;lt; /option &amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;multi_select = Select(dropdown)
 multi_select.select_by_visible_text(“Iowa”)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;select_by_value(value)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This method selects the options whose value corresponds to the value argument.&lt;br&gt;&lt;br&gt;
For example:&lt;br&gt;&lt;br&gt;
&lt;code&gt;&amp;lt; option value=”AZ”&amp;gt; Arizona &amp;lt; /option &amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;multi_select = Select(dropdown)
 multi_select.select_by_value(“AZ”)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Demonstration: How to handle Multiple Values in a Drop-Down Menu using Selenium Python
&lt;/h4&gt;

&lt;p&gt;Let us look at how to automate the selection of multiple values in the following test case scenario.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the &lt;a href="https://www.lambdatest.com/selenium-playground/jquery-dropdown-search-demo?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;jQuery Dropdown Search&lt;/a&gt; demo page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the &lt;strong&gt;Multi Select&lt;/strong&gt; section on the web page to choose multiple states.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select 3 states from the dropdown menu.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Print the states selected.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Test Case on the Chrome Browser):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.support.select import Select


def test_multiple_values_dropdown():
    options = ChromeOptions()
    options.browser_version = "107.0"
    options.platform_name = "Windows 10"
    lt_options = {};
    lt_options["username"] = os.environ.get("LT_USERNAME");
    lt_options["accessKey"] = os.environ.get("LT_ACCESS_KEY");
    lt_options["build"] = "Handling Multiple Values Selection in Dropdowns";
    lt_options["project"] = "Handling Multiple Values Selection in Dropdowns";
    lt_options["name"] = "Handling Multiple Values Selection in Dropdowns";
    lt_options["selenium_version"] = "4.0.0";
    lt_options["w3c"] = True;
    options.set_capability('LT:Options', lt_options);

    # LambdaTest Profile username
    user_name = os.environ.get('LT_USERNAME')
    # LambdaTest Profile access_key
    accesskey = os.environ.get('LT_ACCESS_KEY')
    remote_url =  "https://" + user_name + ":" + accesskey + "@hub.lambdatest.com/wd/hub"
    driver = webdriver.Remote(remote_url, options=options)
    driver.get("https://www.lambdatest.com/selenium-playground/jquery-dropdown-search-demo")

    # Find the multiselect dropdown element in the page
    dropdown = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, '//*[@id="__next"]/div/section[3]/div/div/div[2]/div[2]/div[2]/select')))

    # Using the webdriver Select class to select multiple values
    multi_select = Select(dropdown)
    driver.implicitly_wait(3)
    multi_select.select_by_index(0)
    driver.implicitly_wait(3)
    multi_select.select_by_value("AZ")
    driver.implicitly_wait(3)
    multi_select.select_by_visible_text("Iowa")
    print("All selected values using the SELECT Class in the dropdown are: \n")
    for option in multi_select.all_selected_options:
        print(option.get_attribute('innerText'))

    driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Type the command below in the terminal to run the test script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; python -m pytest test_multiple_values_dropdowns.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test executes successfully and displays the three selected states as the output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AWai0H3T9KXC1OGKo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AWai0H3T9KXC1OGKo.png" alt="image" width="800" height="117"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can view the status of your successful test run on your LambdaTest Automation Dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AUE3rbLHVxdfNiErV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AUE3rbLHVxdfNiErV.png" alt="image" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Inspect web elements to help developers and testers to debug UI flaws or make modifications in HTML or CSS files. Learn&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/software-testing-questions/how-to-inspect-on-macbook?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;how to inspect element on Mac&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;-Book.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; In the test script above, we import the necessary Selenium libraries required to run the script, including the &lt;strong&gt;Select class&lt;/strong&gt; that will automate the selection of multiple options from a drop-down.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A4EuuYZ-BMujvgh_x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A4EuuYZ-BMujvgh_x.png" alt="image" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Navigate to the website where we will run the test script using the &lt;strong&gt;driver.get()&lt;/strong&gt; method. Using the &lt;a href="https://www.lambdatest.com/blog/selenium-wait-for-page-to-load/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Explicit wait&lt;/a&gt; with &lt;strong&gt;WebDriverWait&lt;/strong&gt;, we wait for the drop-down element to appear and then locate it using &lt;strong&gt;XPath&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Ay1XdcFPCdpor2Xlg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Ay1XdcFPCdpor2Xlg.png" alt="image" width="800" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Am8Bx5Wm5PLgO-guo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Am8Bx5Wm5PLgO-guo.png" alt="image" width="800" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; We create an instance of the Select class using the &lt;strong&gt;Select()&lt;/strong&gt; method by passing the &lt;strong&gt;multi_select&lt;/strong&gt; element as its argument. We select multiple values from the drop-down menu using the &lt;strong&gt;select_by_index()&lt;/strong&gt;, &lt;strong&gt;select_by_value()&lt;/strong&gt;, and &lt;strong&gt;select_by_visible_text()&lt;/strong&gt; methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Then, use &lt;strong&gt;all_select_options()&lt;/strong&gt; to iterate through the selected options and print the individual options we chose. The option tag’s text string will be printed by the &lt;strong&gt;get_attribute(“innerText”)&lt;/strong&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AzuNaTbS7LAnW-_eW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AzuNaTbS7LAnW-_eW.png" alt="image" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Finally, calling the &lt;strong&gt;driver.close()&lt;/strong&gt; ends the driver session.&lt;/p&gt;

&lt;h3&gt;
  
  
  jQuery Date Pickers
&lt;/h3&gt;

&lt;p&gt;When automating an airline or hotel website or filling up a form on a website, you might come across a date picker or calendar to pick dates or even time. The jQuery calendar is the most commonly used date picker control. These calendars significantly enhance the user experience by making choosing dates easier and more dynamic.&lt;/p&gt;

&lt;p&gt;jQuery date pickers can pose a challenge when automating web applications using Selenium Python.&lt;/p&gt;

&lt;p&gt;The date pickers could implement multiple date and time formats, making it challenging as the test implementation will need to be adjusted to match the calendar’s style and appearance. The calendar may also require navigation controls such as previous and next buttons to choose different months and years to select the dates. The calendar date pickers may also be rendered differently depending on the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://Booking.com" rel="noopener noreferrer"&gt;Booking.com&lt;/a&gt; datepicker in the Chrome browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AQc6PPVqi5N_gWjti.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AQc6PPVqi5N_gWjti.png" alt="image" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://Booking.com" rel="noopener noreferrer"&gt;Booking.com&lt;/a&gt; datepicker in Firefox browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AJS58BEsFyrpvu2xj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AJS58BEsFyrpvu2xj.png" alt="image" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Demonstration: How to handle a jQuery Calendar in Selenium Python
&lt;/h4&gt;

&lt;p&gt;Let’s look at a scenario on how to automate a jQuery calendar in Selenium Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the &lt;a href="https://www.lambdatest.com/selenium-playground/jquery-date-picker-demo?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;jQuery Date Picker&lt;/a&gt; demo page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the &lt;strong&gt;Date Range Picker&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;strong&gt;From&lt;/strong&gt; date picker field, select 05/10/2023 as the date.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;strong&gt;To&lt;/strong&gt; date picker field, select 06/25/2023 as the date.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Print the dates selected.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AntMHXJMPRANmt-3N.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AntMHXJMPRANmt-3N.png" alt="image" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Test Case on the Chrome Browser):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def test_handling_jquey_datepicker():
    options = ChromeOptions()
    options.browser_version = "107.0"
    options.platform_name = "Windows 10"
    lt_options = {};
    lt_options["username"] = os.environ.get('LT_USERNAME');
    lt_options["accessKey"] = os.environ.get('LT_ACCESS_KEY');
    lt_options["build"] = "Handling Date Pickers";
    lt_options["project"] = "Handling Date Pickers";
    lt_options["name"] = "Handling Date Pickers";
    lt_options["w3c"] = True;
    lt_options["plugin"] = "python-python";
    options.set_capability('LT:Options', lt_options)
    # LambdaTest Profile username
    user_name = os.environ.get('LT_USERNAME')
    # LambdaTest Profile access_key
    accesskey = os.environ.get('LT_ACCESS_KEY')
    remote_url =  "https://" + user_name + ":" + accesskey + "@hub.lambdatest.com/wd/hub"
    driver = webdriver.Remote(remote_url, options=options)
    # Handling JQuery DatePicker
    driver.get('https://www.lambdatest.com/selenium-playground/jquery-date-picker-demo')

    # expected dates to be selected
    from_date_target = '05/10/2023'
    to_date_target = '06/25/2023'

    from_date = '10'
    to_date  = '25'


    # From date
    # clicking on the from_date picker
    from_date_picker = driver.find_element(By.XPATH, "//input[@id='from']")
    from_date_picker.click()

    from_month = driver.find_element(By.XPATH, "//select[@class='ui-datepicker-month']")
    # We use the Select() method to select the target month we want
    from_month_selected = Select(from_month)
    from_month_selected.select_by_visible_text('May')

    from_day = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH,"//td[not(contains(@class,'ui-datepicker-month'))]/a[text()='" + from_date + "']")))
    from_day.click()


    # To date
    to_date_picker = driver.find_element(By.XPATH, "//input[@id='to']")
    to_date_picker.click()

    to_month = driver.find_element(By.XPATH, "//select[@class='ui-datepicker-month']")
    to_month_selected = Select(to_month)
    to_month_selected.select_by_visible_text('Jun')    

    to_day = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH,"//td[not(contains(@class,'ui-datepicker-month'))]/a[text()='" + to_date + "']")))
    to_day.click()


    # Assert the selected dates
    expected_from_date = from_date_picker.get_attribute('value')
    if expected_from_date == from_date_target:
        print("From date selected:", expected_from_date)

    expected_to_date = to_date_picker.get_attribute('value')
    if expected_to_date == to_date_target:
        print("To date selected:", expected_to_date)

    driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To run the test script above in the terminal, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pytest test_handling_datepickers.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test script executes successfully and will display the following output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AAUumGOd_PlPYyfbX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AAUumGOd_PlPYyfbX.png" alt="image" width="800" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also view test results on the LambdaTest Dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AAoepspKkdx55k8Yi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AAoepspKkdx55k8Yi.png" alt="image" width="800" height="578"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Video recording of &lt;strong&gt;From&lt;/strong&gt; date selection:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ArVp-Dvi_U3EDhtIO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ArVp-Dvi_U3EDhtIO.png" alt="image" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Video recording of &lt;strong&gt;To&lt;/strong&gt; date selection:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2An047Im4tXrkoaS6t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2An047Im4tXrkoaS6t.png" alt="image" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ADuTrDICF8qO56QJU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ADuTrDICF8qO56QJU.png" alt="image" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Import the &lt;strong&gt;Select class&lt;/strong&gt; from Selenium WebDriver to help us implement the &lt;code&gt;&amp;lt; select &amp;gt;&lt;/code&gt; tag, which provides the helper methods with select and deselect options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AEc6_4tQ3CvVz3ETV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AEc6_4tQ3CvVz3ETV.png" alt="image" width="800" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Define the &lt;strong&gt;from_date target&lt;/strong&gt; and &lt;strong&gt;to_date_target&lt;/strong&gt; date targets that we want to select from the datepicker.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AQfMo3qOap_qE4xIV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AQfMo3qOap_qE4xIV.png" alt="image" width="772" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; The &lt;strong&gt;from_date_picker&lt;/strong&gt; element is located using XPath in the Chrome Developer Tools and clicked to open the calendar.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Ad3JrClsDyA1hha8C.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Ad3JrClsDyA1hha8C.png" alt="image" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; From the calendar, we locate &lt;strong&gt;from_month&lt;/strong&gt; using XPath and select the month using the &lt;strong&gt;Select()&lt;/strong&gt; method. In this instance, we select the month of May using the &lt;strong&gt;select_by_visible_text()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Since we have specified the precise value of the &lt;strong&gt;from_date&lt;/strong&gt;, the 10th day is located from the month using a dynamic XPath, and the &lt;strong&gt;contains()&lt;/strong&gt; method. We then click on the day.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AkJFPg3Z3Ix_OXaas.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AkJFPg3Z3Ix_OXaas.png" alt="image" width="800" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Given that the datepicker provides date range capability, the procedures implemented for the &lt;strong&gt;from_date&lt;/strong&gt; are also applied to the &lt;strong&gt;to_date&lt;/strong&gt;. The only difference comes from the months and locators used to locate the &lt;strong&gt;“from”&lt;/strong&gt; and &lt;strong&gt;“to”&lt;/strong&gt; dates. We selected June and the sixth day for the &lt;strong&gt;to_ date&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ARGpkL4MrDTzuxCdt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ARGpkL4MrDTzuxCdt.png" alt="image" width="800" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Finally, we ascertain that the &lt;strong&gt;from_date_picker&lt;/strong&gt; and the &lt;strong&gt;to_date_picker&lt;/strong&gt; variables have the exact value we specified in the &lt;strong&gt;from_date_target&lt;/strong&gt; and the &lt;strong&gt;to_date_target&lt;/strong&gt; variables using the &lt;strong&gt;get_attribute(‘value’)&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;We store these values in the &lt;strong&gt;expected_from_date&lt;/strong&gt; and &lt;strong&gt;expected_to_date&lt;/strong&gt; variables and print the output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7&lt;/strong&gt;: We then close the WebDriver session with the &lt;strong&gt;driver.close()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AdwrrZuL9RrA33PVb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AdwrrZuL9RrA33PVb.png" alt="image" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Mouse Hover Elements
&lt;/h3&gt;

&lt;p&gt;A mouse hover action is an action taken when a user moves a mouse cursor over a designated part of a webpage, and it triggers a specific event. For example, in the Selenium eCommerce Playground website, hovering over a product element will show a sub-menu of a user’s actions, such as the “Add to Cart” function.&lt;/p&gt;

&lt;p&gt;Mouse hover can also be used on banks’ websites to show different services the bank offers, as well as on hotel websites to zoom in on a location’s picture or add the location to likes.&lt;/p&gt;

&lt;p&gt;Mouse hover on product item showing “Add to Cart” action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AD3935fjiejNHoBNb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AD3935fjiejNHoBNb.png" alt="image" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mouse hover on the bank website showing the bank’s services:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AcgJV4GL35EFAJY0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AcgJV4GL35EFAJY0h.png" alt="image" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hover elements can pose a challenge while writing automated tests using Selenium Python. Some elements are often hidden and only become visible when the mouse hovers over some other element, making it difficult to locate them using the standard &lt;strong&gt;find_element()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;Additionally, hover events can be unstable as they depend on the mouse movement speed and the timing of triggering the hover event. In the case of slow internet speeds, the mouse cursor movements might lag. This may result in the tests being flaky.&lt;/p&gt;

&lt;p&gt;To handle mouse hover actions, we implement the methods from the &lt;strong&gt;ActionChains&lt;/strong&gt; class. The class methods will simulate the mouse movement.&lt;/p&gt;

&lt;h4&gt;
  
  
  Demonstration: How to handle Mouse Hover Actions in Selenium Python
&lt;/h4&gt;

&lt;p&gt;Let us look at a test case scenario that requires handling a mouse hover action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the LambdaTest Selenium eCommerce Playground.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to product items and hover the mouse on the item.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the &lt;strong&gt;Add to Cart&lt;/strong&gt; button that hovers over the product item.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Print that the item has been added to the cart successfully.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Test Case on the Chrome Browser):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

def test_mouse_hover():
    options = ChromeOptions()
    options.browser_version = "107.0"
    options.platform_name = "Windows 10"
    lt_options = {};
    lt_options["username"] = os.environ.get("LT_USERNAME");
    lt_options["accessKey"] = os.environ.get("LT_ACCESS_KEY");
    lt_options["build"] = "MouseHoverHandling";
    lt_options["project"] = "MouseHoverHandling";
    lt_options["name"] = "MouseHoverHandling";
    lt_options["selenium_version"] = "4.0.0";
    lt_options["w3c"] = True;
    options.set_capability('LT:Options', lt_options);

    # LambdaTest Profile username
    user_name = os.environ.get('LT_USERNAME')
    # LambdaTest Profile access_key
    accesskey = os.environ.get('LT_ACCESS_KEY')
    remote_url =  "https://" + user_name + ":" + accesskey + "@hub.lambdatest.com/wd/hub"
    driver = webdriver.Remote(remote_url, options=options)

    driver.get("https://ecommerce-playground.lambdatest.io/")
    driver.implicitly_wait(10)
    # Find the element we want to hover
    product_item = driver.find_element(By.XPATH, '//*[@id="mz-product-listing-image-37213259-0-0"]/div/div[1]/img')

    # Create a sequence of actions through the ActionsChains class
    driver.implicitly_wait(10)
    actions = ActionChains(driver)
    # Perform triggers the actions
    actions.move_to_element(product_item).perform()
    driver.implicitly_wait(10)

    # find the add_to_cart element we want to click on the product elementa
    add_to_cart = driver.find_element(By.CSS_SELECTOR, 'button[title="Add to Cart"]')
    # The mouse will click the add_to_cart element to complete the sequence.
    driver.implicitly_wait(10)
    actions.click(add_to_cart)
    print("Item moved to the cart")

    driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Type the following command in the terminal to run the test script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pytest test_mouse_hover.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test successfully executes on the terminal, displaying the output as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AwM1Ap59uKnQAYoD-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AwM1Ap59uKnQAYoD-.png" alt="image" width="800" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The test results can be viewed in the LambdaTest Dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AHk3TCMvby87hYVu1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AHk3TCMvby87hYVu1.png" alt="image" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AN8W4mvAnC6CGx7As.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AN8W4mvAnC6CGx7As.png" alt="image" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Import the &lt;strong&gt;ActionChains&lt;/strong&gt; class from the Selenium WebDriver, which provides a variety of APIs for keyboard and mouse actions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AyF6LgooGAo7TZ_AZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AyF6LgooGAo7TZ_AZ.png" alt="image" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Navigate to the Selenium eCommerce Playground website using the &lt;strong&gt;driver.get()&lt;/strong&gt; method. We first locate a product element in the webpage using its XPath from the Chrome Browser’s Inspect Tools to display the hover effect.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AUN1E_uaBsM2oo6AI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AUN1E_uaBsM2oo6AI.png" alt="image" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A43x1TcYXrWslfXo0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A43x1TcYXrWslfXo0.png" alt="image" width="800" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Using the ActionChains class, we create an object of the class and use the &lt;strong&gt;move_to_element()&lt;/strong&gt; method with the &lt;strong&gt;product_item&lt;/strong&gt; element as its parameter. The &lt;strong&gt;move_to_element()&lt;/strong&gt; method moves the mouse to the middle of the element. Then apply the &lt;strong&gt;perform()&lt;/strong&gt; method to implement the mouse action.&lt;/p&gt;

&lt;p&gt;Once the mouse cursor is on the &lt;strong&gt;product_item&lt;/strong&gt; element, we locate the &lt;strong&gt;add_to_cart&lt;/strong&gt; button using its CSS Selector. The &lt;strong&gt;add_to_cart&lt;/strong&gt; element becomes visible when the mouse hovers over the &lt;strong&gt;product_item&lt;/strong&gt; and then clicks on it using the actions class object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AsvsLzJHNmUvFpUy4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AsvsLzJHNmUvFpUy4.png" alt="image" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Auz8v3dsoET4-nbrK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Auz8v3dsoET4-nbrK.png" alt="image" width="800" height="329"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; We finally close the webdriver session using the &lt;strong&gt;driver.close()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Perform manual or automated cross&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;browser test&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;on 3000+ browsers online. Deploy and scale faster with the most powerful cross browser testing tool online.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  AJAX Form Submission
&lt;/h3&gt;

&lt;p&gt;AJAX forms are a challenge in automation testing using Selenium Python because of the asynchronous nature of AJAX calls. AJAX calls can be used in web apps to enable features such as autocomplete search, which displays results as you type, or infinite scrolling to fetch additional content in social media apps without reloading the page.&lt;/p&gt;

&lt;p&gt;When you fill in and submit the traditional forms, the page reloads, and the refreshed page can be interacted with easily using Selenium. AJAX forms, on the other hand, do not require a page reload because the form data is delivered and handled asynchronously in the background. As new content is received from the server, a part of the web page’s &lt;strong&gt;DOM&lt;/strong&gt; is updated rather than the entire DOM state changing. This can make it difficult to determine whether the form has completed processing and if the refreshed page content is accessible to be interacted with through Selenium.&lt;/p&gt;

&lt;p&gt;To automate handling AJAX form submissions, we use the explicit wait functions to wait for the refreshed page to load before proceeding with the code. On the other hand, implicit waits are not recommended for handling AJAX form submissions because they do not wait for a specific form submission to complete before continuing with the test script. The test script will continue to execute without waiting for the request to complete. This causes the test to be flaky because of wait time issues.&lt;/p&gt;

&lt;h4&gt;
  
  
  Demonstration: How to handle AJAX Form Submission using Selenium Python
&lt;/h4&gt;

&lt;p&gt;To demonstrate handling AJAX form submissions, we will use the AJAX contact form by &lt;a href="http://Getform.io" rel="noopener noreferrer"&gt;Getform.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the Getform CodePen URL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fill in the form details and click on the &lt;strong&gt;Submit&lt;/strong&gt; button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wait for the submission response and print it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AbQ-f6LiqymxUD4Q8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AbQ-f6LiqymxUD4Q8.png" alt="image" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Test Case on the Chrome Browser):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.support.select import Select


def test_AJAX_forms():
    options = ChromeOptions()
    options.browser_version = "107.0"
    options.platform_name = "Windows 10"
    lt_options = {};
    lt_options["username"] = os.environ.get("LT_USERNAME");
    lt_options["accessKey"] = os.environ.get("LT_ACCESS_KEY");
    lt_options["build"] = "Handling AJAX Forms";
    lt_options["project"] = "Handling AJAX Forms";
    lt_options["name"] = "Handling AJAX Forms";
    lt_options["w3c"] = True;
    lt_options["plugin"] = "python-python";
    options.set_capability('LT:Options', lt_options);

    # LambdaTest Profile username
    user_name = os.environ.get('LT_USERNAME')
    # LambdaTest Profile access_key
    accesskey = os.environ.get('LT_ACCESS_KEY')
    remote_url =  "https://" + user_name + ":" + accesskey + "@hub.lambdatest.com/wd/hub"
    driver = webdriver.Remote(remote_url, options=options)

    driver.get("https://codepen.io/getform/pen/jRoexL")

    # Get into the iFrame
    driver.switch_to.frame(driver.find_element(By.TAG_NAME, "iframe"))

    # Locate the Form
    form = driver.find_element(By.XPATH, '//*[@id="AJAXForm"]')
    # Identify the forms inputs and enter data through send_keys  
    email_element = form.find_element(By.XPATH, '//*[@id="exampleInputEmail1"]')
    email_element.send_keys('youremail@gmail.com')
    name_element = form.find_element(By.XPATH, '//*[@id="exampleInputName"]')
    name_element.send_keys('yourname')
    # Locate the dropdown
    platform_element = Select(form.find_element(By.XPATH, '//*[@id="exampleFormControlSelect1"]'))
    platform_element.select_by_visible_text('Github')
    submit = form.find_element(By.XPATH, '//*[@id="AJAXForm"]/button')
    submit.click()

    # Wait for the AJAX request to complete
    response = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="AJAXForm"]/div[4]')))
    print(response.text)

    driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let us run the test script in the terminal using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pytest test_handling_AJAX_forms.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test executes successfully, and the terminal displays the form’s response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ABF_2bvUnWhwsnWz6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ABF_2bvUnWhwsnWz6.png" alt="image" width="800" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can view the test results in the LambdaTest Automation Dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AKC9Hi_9pm_q8B2Xb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AKC9Hi_9pm_q8B2Xb.png" alt="image" width="800" height="549"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; To get started with the automated testing of an AJAX form submission test scenario in Selenium Python, we import the necessary Selenium libraries, including &lt;strong&gt;WebDriverWait&lt;/strong&gt;, and the &lt;strong&gt;expected conditions&lt;/strong&gt; for the explicit wait. The explicit wait will enable the WebDriver to wait until the form submission returns the response, and it becomes visible in the DOM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; We also import the &lt;strong&gt;Select&lt;/strong&gt; class to handle the drop-down input menu in the form.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Ajy9gJjp68_OY-wN7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Ajy9gJjp68_OY-wN7.png" alt="image" width="800" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; The AJAX form is inside an iFrame, and the test script cannot continue to run until we switch the WebDriver’s context to the iFrame. In the Inspect Tool, the iFrame is identified by its TAG_NAME. We then switch to the iFrame using the &lt;strong&gt;driver.switch_to.frame()&lt;/strong&gt; method with the iFrame element as its parameter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ABmsCFgcmuR58uzrZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ABmsCFgcmuR58uzrZ.png" alt="image" width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AutVm1kkJi4Ufpr6e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AutVm1kkJi4Ufpr6e.png" alt="image" width="800" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; After switching to the iFrame, we locate the form using XPath from the Inspect Tools. After locating the email and name inputs, we use the &lt;strong&gt;send_keys()&lt;/strong&gt; method to fill in the email and name details.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; The &lt;strong&gt;Select&lt;/strong&gt; class we imported will select an option from the &lt;strong&gt;platform_element&lt;/strong&gt; drop-down menu using the &lt;strong&gt;select_by_visible_text()&lt;/strong&gt; method. Finally, click on the &lt;strong&gt;Submit&lt;/strong&gt; button to submit the form details.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Aqi-F-TtbFQW2on7t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2Aqi-F-TtbFQW2on7t.png" alt="image" width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AE1W9gc5DFcFXbL5a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AE1W9gc5DFcFXbL5a.png" alt="image" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; After submitting the form, we use &lt;strong&gt;explicit wait&lt;/strong&gt; to wait for the web page to load asynchronously until the response element is present in the DOM. Then print the response’s text.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AZxLAF9jMe8H-Wz2T.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AZxLAF9jMe8H-Wz2T.png" alt="image" width="800" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; The &lt;strong&gt;driver.quit()&lt;/strong&gt; closes the driver session.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dynamic Page Elements
&lt;/h3&gt;

&lt;p&gt;If an element in a web application has attributes such as Class, ID, or XPath that change when the web application is reloaded, such elements are known as &lt;strong&gt;dynamic web elements&lt;/strong&gt;. For example, the login web element ID on the Facebook login page changes every time the page is reloaded.&lt;/p&gt;

&lt;p&gt;In the below image, the login button ID is “u 0 5 aj.”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AnwS0YuRhOtm17lxp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AnwS0YuRhOtm17lxp.png" alt="image" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the page is reloaded, the login button ID is now “u 0 5 Hg.”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AyiVWrK153ZjtU0rf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AyiVWrK153ZjtU0rf.png" alt="image" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Dynamic content can be challenging to automate with Selenium Python because it requires the automated test to identify and interact with the dynamic web elements. When a dynamic page element on a web page loads, not all elements are available immediately. When the WebDriver tries to interact with an element before it is present, it results in a &lt;strong&gt;NoSuchElementException.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stale elements, on the other hand, are elements that are no longer attached to the web page’s DOM because the page has been reloaded or the element has been removed and replaced by another element. If the WebDriver tries to interact with a stale element, it results in a &lt;strong&gt;StaleElementReferenceException.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can learn more about it through this blog on &lt;a href="https://www.lambdatest.com/blog/handling-errors-and-exceptions-in-selenium-python/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;handling Errors and Exceptions in Selenium Python&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To address this challenge, automated test scripts need to implement &lt;strong&gt;explicit waits&lt;/strong&gt; to wait until the page has finished loading before interacting with elements, as well as dynamic locators to locate and interact with elements that may not exist when the test is initially executed.&lt;/p&gt;

&lt;h4&gt;
  
  
  Demonstration: How to handle Dynamic Content with Selenium Python
&lt;/h4&gt;

&lt;p&gt;Let us look at a test scenario to handle dynamic content on a website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the Internet HerokuApp link.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the &lt;strong&gt;Start&lt;/strong&gt; button and wait for the web page to load.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Print the response displayed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AomBsPJomMZnckttL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AomBsPJomMZnckttL.png" alt="image" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Test Case on the Chrome Browser):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options as ChromeOptions


def test_dynamic_content():
    options = ChromeOptions()
    options.browser_version = "107.0"
    options.platform_name = "Windows 10"
    lt_options = {};
    lt_options["username"] = os.environ.get("LT_USERNAME");
    lt_options["accessKey"] = os.environ.get("LT_ACCESS_KEY");
    lt_options["build"] = "Handling Dynamic Content";
    lt_options["project"] = "Handling Dynamic Content";
    lt_options["name"] = "Handling Dynamic Content";
    lt_options["w3c"] = True;
    lt_options["plugin"] = "python-python";
    options.set_capability('LT:Options', lt_options);

    # LambdaTest Profile username
    user_name = os.environ.get('LT_USERNAME')
    # LambdaTest Profile access_key
    accesskey = os.environ.get('LT_ACCESS_KEY')
    remote_url =  "https://" + user_name + ":" + accesskey + "@hub.lambdatest.com/wd/hub"
    driver = webdriver.Remote(remote_url, options=options)

    driver.get("https://the-internet.herokuapp.com/dynamic_loading/2")
    # locate the button start to dynamically
    start_button = driver.find_element(By.XPATH, '//*[@id="start"]/button')
    start_button.click()
    # Waiting for the text to appear after clicking the start_button
    finish_element = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="finish"]/h4')))
    print(finish_element.text)


    driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run the command below in the terminal to execute the test script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pytest test_handling_dynamic_content.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the test runs, the following output will be displayed in the terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AkzMJOm7CVIyL53iL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AkzMJOm7CVIyL53iL.png" alt="image" width="800" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see the status of the test execution and logs in the LambdaTest Automation Dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AOtORODYnlH_Fkbke.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AOtORODYnlH_Fkbke.png" alt="image" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Import the necessary Selenium libraries to run the test script, including the &lt;strong&gt;WebDriverWait&lt;/strong&gt; class and &lt;strong&gt;expected_conditions&lt;/strong&gt; that provide the explicit wait.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A5zwfonVAED6-sK87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2A5zwfonVAED6-sK87.png" alt="image" width="800" height="303"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The WebDriver navigates to the website using the &lt;strong&gt;driver.get()&lt;/strong&gt; method, locates the &lt;strong&gt;start_button&lt;/strong&gt; element using its XPath in the Inspect Tools, and clicks on it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ASrsBNI8p7sO1OlW_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ASrsBNI8p7sO1OlW_.png" alt="image" width="800" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AGgBDebB2hQGQbK98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AGgBDebB2hQGQbK98.png" alt="image" width="800" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Using the &lt;strong&gt;WebDriverWait&lt;/strong&gt; class, the WebDriver waits for the visibility of the response &lt;strong&gt;(finish_element)&lt;/strong&gt; using an explicit wait. Once the element is present in the DOM, we print its text. The WebDriver will raise a &lt;strong&gt;TimeoutException&lt;/strong&gt;, and the code will stop running if the &lt;strong&gt;finish_element&lt;/strong&gt; is not visible in the DOM after the explicit wait period. The exception can be caught using a &lt;strong&gt;try-except&lt;/strong&gt; block.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AjumkkJauvQBHbjjO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AjumkkJauvQBHbjjO.png" alt="image" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Finally, the &lt;strong&gt;driver.close()&lt;/strong&gt; method ends the WebDriver session.&lt;/p&gt;

&lt;h3&gt;
  
  
  Uploading Files
&lt;/h3&gt;

&lt;p&gt;Uploading a file involves searching for a file at the required location or on your computer and uploading it to the website. This action can be done on specific websites, such as forms, document uploaders, and registration pages.&lt;/p&gt;

&lt;p&gt;Uploading files can be challenging when performing automation tests with Selenium Python. The file dialog box that appears when you click the upload button is a native OS-level component and can be difficult to interact with programmatically, making it difficult to simulate the file selection process.&lt;/p&gt;

&lt;p&gt;Additionally, different web applications might support various file formats for upload, and the same file upload element might behave differently depending on the file type. This can make writing reliable tests that function across several programs and file types difficult.&lt;/p&gt;

&lt;p&gt;To handle this challenge, we will use the &lt;strong&gt;send_keys()&lt;/strong&gt; method to upload the file. This approach is applicable to file upload elements with the tagname &lt;strong&gt;input&lt;/strong&gt; and the type attribute set to the &lt;strong&gt;file&lt;/strong&gt;. The &lt;strong&gt;send_keys()&lt;/strong&gt; method will take the absolute path of the file as its argument and upload it to the web application we are testing.&lt;/p&gt;

&lt;h4&gt;
  
  
  Demonstration: How to Upload a File with Selenium Python
&lt;/h4&gt;

&lt;p&gt;Let us look at a scenario for uploading a file when conducting automation testing with Selenium Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the jQuery File Upload Demo page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Locate and click on the &lt;strong&gt;Add files&lt;/strong&gt; button on the webpage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose a file you want to upload and click on &lt;strong&gt;Open&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the &lt;strong&gt;Start&lt;/strong&gt; button to upload the file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Print that the file has been uploaded successfully.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Test Case on a Chrome Browser):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def test_file_uploads():
    options = ChromeOptions()
    options.browser_version = "107.0"
    options.platform_name = "Windows 10"
    lt_options = {};
    lt_options["username"] = os.environ.get("LT_USERNAME");
    lt_options["accessKey"] = os.environ.get("LT_ACCESS_KEY");
    lt_options["build"] = "Handling File Upload";
    lt_options["project"] = "Handling File Upload";
    lt_options["name"] = "Handling File Upload";
    lt_options["w3c"] = True;
    lt_options["plugin"] = "python-python";
    options.set_capability('LT:Options', lt_options);

    # LambdaTest Profile username
    user_name = os.environ.get('LT_USERNAME')
    # LambdaTest Profile access_key
    accesskey = os.environ.get('LT_ACCESS_KEY')
    remote_url =  "https://" + user_name + ":" + accesskey + "@hub.lambdatest.com/wd/hub"
    driver = webdriver.Remote(remote_url, options=options)
    # Access website to automate file upload
    driver.get("https://blueimp.github.io/jQuery-File-Upload/")
    # locate the file upload button on the page using XPATH
    file_upload = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, "//input[@name='files[]']")))
    # The Send Keys method will include the path for the file that will be uploaded.
    file_upload.send_keys(r"C:\Users\Pictures\Saved Pictures\logo.jpg")

    start_upload = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH,'//*[@id="fileupload"]/table/tbody/tr/td[4]/button[2]')))
    start_upload.click()

    # is_displayed method is used to check if an element is visible to the user or not.
    if driver.find_element(By.XPATH, '//*[@id="fileupload"]/table').is_displayed:
        print("file uploaded successfully")
    else:
        print("File not uploaded")

    driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let us run the test script in the terminal with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pytest test_upload_files.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test executes successfully and returns the following output in the terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AoZM7OQg_rdk7qXg9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AoZM7OQg_rdk7qXg9.png" alt="image" width="800" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The test execution status can be viewed on the LambdaTest Automation Dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AkfxAGMRVKvgyQecX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AkfxAGMRVKvgyQecX.png" alt="image" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; After creating an instance of the WebDriver, we navigate to the URL link using the &lt;strong&gt;driver.get()&lt;/strong&gt; method. The &lt;strong&gt;file_upload&lt;/strong&gt; element is then located using XPath. To upload a file, the &lt;strong&gt;send_keys()&lt;/strong&gt; method takes an absolute path from the operating system’s directory, where the file is stored as its argument. The &lt;strong&gt;file_upload&lt;/strong&gt; element has a tagname of &lt;strong&gt;input&lt;/strong&gt; and a type attribute of the &lt;strong&gt;file&lt;/strong&gt;; hence, we could use the &lt;strong&gt;send_keys()&lt;/strong&gt; method to upload the file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AJvFKD_Yi540o7jmM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AJvFKD_Yi540o7jmM.png" alt="image" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AXfEgF3UdIF1q2f43.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AXfEgF3UdIF1q2f43.png" alt="image" width="800" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; We locate the &lt;strong&gt;start_upload&lt;/strong&gt; button element by using an explicit wait to wait for the element to appear on the DOM after the file is selected from the file upload window. Once the button appears, we click on it to upload the file to the website.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ACMGswMsVlhQOg8bV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ACMGswMsVlhQOg8bV.png" alt="image" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ALpRLi3EPuX-C33ga.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2ALpRLi3EPuX-C33ga.png" alt="image" width="800" height="117"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; To check if the file uploaded is in the web application’s DOM, we use the &lt;strong&gt;is_displayed()&lt;/strong&gt; method. If the uploaded file is present, we print that the file has been uploaded successfully. If not, the test prints that the file has not been uploaded. Finally, the WebDriver is closed using the &lt;strong&gt;driver.close()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AaZ710NLAr_EQ18WZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AaZ710NLAr_EQ18WZ.png" alt="image" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AwQ9T7qqo1yhHNiS6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AwQ9T7qqo1yhHNiS6.png" alt="image" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to automate user actions on a website to verify its functionality, commands for WebElements in Selenium Python play an important role. These commands can help you automate &lt;a href="https://www.lambdatest.com/blog/website-testing/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;website testing&lt;/a&gt; in a more detailed manner. It’s recommended to run Selenium tests on real browsers, and operating systems will help you get accurate results.&lt;/p&gt;

&lt;p&gt;Digital experience testing platforms like LambdaTest enables developers and QA to perform automated &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium testing&lt;/a&gt; using Python on an online &lt;a href="https://www.lambdatest.com/online-browser-farm?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;browser farm&lt;/a&gt; of 3000+ real browsers and OS combinations. LambdaTest offers support for different &lt;a href="https://www.lambdatest.com/blog/top-python-frameworks-for-automation/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Python testing frameworks&lt;/a&gt;, including pytest, Robot, Behave, and more. In addition, you can perform parallel test execution to expedite your software release cycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your native, hybrid, and web apps across all legacy and latest mobile operating systems on the most powerful&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_ap&amp;amp;utm_term=ap&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;online Android emulator&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this blog on WebElements in Selenium Python, we have demonstrated different ways to handle WebElements in Selenium Python. These include handling alert pop-ups, drag-and-drop operations, jQuery datepickers, hover elements, AJAX forms, etc. We have also looked at the approaches to handle them, such as the use of explicit waits, the ActionChains class, Select, and send_keys methods.&lt;/p&gt;

</description>
      <category>python</category>
      <category>selenium</category>
      <category>webelements</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How To Handle Errors And Exceptions In Selenium Python</title>
      <dc:creator>EugeneKwaka</dc:creator>
      <pubDate>Thu, 01 Dec 2022 14:34:00 +0000</pubDate>
      <link>https://forem.com/testmuai/how-to-handle-errors-and-exceptions-in-selenium-python-12ea</link>
      <guid>https://forem.com/testmuai/how-to-handle-errors-and-exceptions-in-selenium-python-12ea</guid>
      <description>&lt;p&gt;In the world of automation testing, Selenium is a free and open-source framework used to perform &lt;a href="https://www.lambdatest.com/web-application-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;web application testing&lt;/a&gt; in web browsers like Chrome, Safari, Firefox, Opera, and Edge. You can write &lt;a href="https://www.lambdatest.com/selenium-ide-cloud?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium IDE tests&lt;/a&gt; using various programming languages such as Python, Java, JavaScript(Node.js), Kotlin, Ruby, PHP, and C#.&lt;/p&gt;

&lt;p&gt;While the &lt;a href="https://www.lambdatest.com/blog/17-key-benefits-of-automation-testing-for-a-successful-release/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;advantages of automated testing&lt;/a&gt; using Selenium are endless, there are incidences of errors and exceptions that happen regularly in the testing tool (or framework). When a program’s execution is disrupted during run-time, an exception is raised, preventing further instructions from being executed. Runtime errors are raised when you perform an unanticipated operation during runtime.&lt;/p&gt;

&lt;p&gt;Exception and error handling is a programming mechanism by which the occurrence of errors and exceptions that halt the execution of a program are responded to by the program to ensure the normal execution of code.&lt;/p&gt;

&lt;p&gt;In this blog on handling exceptions in Selenium Python, we will look at the variety of exceptions and errors that can happen when a Selenium test is running. By the end of this blog, you will be able to implement error and exception handling for &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium automation tests&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So, let’s get started!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Try an online &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium testing&lt;/a&gt; Grid to run your browser automation testing scripts. Our cloud infrastructure has 3000+ desktop &amp;amp; mobile environments. Try for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Errors and Exceptions in Selenium Python
&lt;/h2&gt;

&lt;p&gt;For a program or application to run, a developer must write code that tests every aspect of its functionality. However, the instructions outlined in the code sometimes do not execute as intended.&lt;/p&gt;

&lt;p&gt;For QA engineers, the same rule holds when writing code used to test the features and functionality of the applications that have been created to ensure that the application is ready for production and then shipped to the users.&lt;/p&gt;

&lt;p&gt;Here is an analogy to help you better understand errors and &lt;a href="https://www.lambdatest.com/blog/49-common-selenium-exceptions-automation-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;exceptions in Selenium Python&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Imagine you have a work meeting on Zoom or any online video conference application while at home. During the meeting, the electricity was cut off, which is reported to have affected the whole city. You have no choice but to call off the meeting and wait for the electricity to be reinstated. Akin to this, errors are raised during runtime, and rectifying them is nearly impossible.&lt;/p&gt;

&lt;p&gt;In contrast, exceptions are events that raise interruptions while an application runs and can be rectified using exception handling.&lt;/p&gt;

&lt;p&gt;Exceptions in Selenium Python are generally classified into two types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Checked Exceptions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unchecked Exceptions&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Checked Exception&lt;/strong&gt; handling in Selenium is conducted while writing the code. On the other hand, &lt;strong&gt;Unchecked Exceptions&lt;/strong&gt; are raised during runtime and can generate fatal errors compared to the former type of exception.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AMmzuvDa-rmfAeTWk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AMmzuvDa-rmfAeTWk.png" alt="***The class hierarchy of error and exceptions in Selenium Python***"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can quickly sharpen your knowledge of exceptions in Selenium Python by reading through this detailed blog on &lt;a href="https://www.lambdatest.com/blog/49-common-selenium-exceptions-automation-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;different exceptions in Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Performing Selenium automated browser testing differs across the ecosystem in which the tests are being carried out. The test scripts may differ depending on the operating systems, browsers, or programming languages they are written in. Therefore, the raised errors andexceptions in Selenium Python may be different. For example, if the browsers don’t have the specified element, Selenium testing of an element property in web browsers may raise exceptions like NoSuchElementException.&lt;/p&gt;

&lt;p&gt;To ensure that the applications function correctly and obstruct users’ interactions with the finished product, it is advised that Selenium error and exception handling be used as a standard practice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test on &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Automation&lt;/a&gt; Grid Cloud of 3000+ Desktop &amp;amp; Mobile Browsers.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Exceptions in Selenium Python
&lt;/h2&gt;

&lt;p&gt;According to a recent Stack Overflow developer survey, Python is the fourth most popular programming language among developers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AC9G8UKJpdhFXOMKo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AC9G8UKJpdhFXOMKo.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It offers a robust support system for &lt;a href="https://www.lambdatest.com/blog/best-test-automation-frameworks-2021/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test automation frameworks&lt;/a&gt; and is the most user-friendly and straightforward programming language available.&lt;/p&gt;

&lt;p&gt;Errors and exceptions in Selenium Python are common and can occur when writing tests. The exception instances must be derived from a class that inherits from &lt;strong&gt;BaseException&lt;/strong&gt;. If two exception instances do not have a subclass relationship, they are not considered equal despite having the same properties, such as names.&lt;/p&gt;

&lt;p&gt;While writing code for the test scripts, the code can run into built-in exceptions, or you can raise exceptions in the code. The interpreter or built-in functions generate the built-in exceptions.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;selenium.common.exceptions&lt;/strong&gt; package contains the exception classes, which can be imported in the following way.&lt;/p&gt;

&lt;p&gt;Syntax: &lt;strong&gt;from selenium.common.exceptions import [Exception Name]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An example of an exception being imported is below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2A_kJpdRBhmftrY881.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2A_kJpdRBhmftrY881.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This exception indicates that the website does not contain the attribute of the element being looked for.&lt;/p&gt;

&lt;p&gt;Other exceptions in Selenium Python include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;TimeOutException&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2ADOhvOsYtxYRhkwzS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2ADOhvOsYtxYRhkwzS.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a command does not complete in the time duration set (seconds), this exception is raised or thrown. The wait time can either be implicitly or explicitly defined.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;NoSuchElementException&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AFUn5xSsPWNUB7YJH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AFUn5xSsPWNUB7YJH.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When an element cannot be found, this exception is raised or thrown. Nonetheless, different factors can lead to this exception being generated:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The element is not yet in the DOM when the find operation is performed because the page loading is in progress.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Incorrect element locator is used in the source code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ElementNotInteractableException&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AtK9aBWVPKmRwpYtr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AtK9aBWVPKmRwpYtr.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Such exceptions in Selenium Python is raised whenever an element in the DOM is present but cannot be interacted with.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;ElementNotVisibleException&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AppE6GC2N8F6Wxg2J.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AppE6GC2N8F6Wxg2J.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This exception typically arises when the interaction involves clicking or reading text from an element that is not visible in the DOM.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;StaleElementReferenceException&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AYf_Ep41h4O5KBkhI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AYf_Ep41h4O5KBkhI.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An outdated or unavailable element in the DOM is referred to as a stale element. A reference to an element that is currently “stale” will raise or throw this exception.&lt;/p&gt;

&lt;p&gt;For example, the following reasons could lead to the StaleElementReferenceException exception:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;After the element was located, the page either changed or was refreshed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A refreshed context may have contained the element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Complete deletion of the referenced web element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ElementClickInterceptedException&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AuPTkTwEbWrNM6hlG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AuPTkTwEbWrNM6hlG.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This exception arises when the element receiving the interaction impedes the element intended to be clicked, thereby preventing the Element Click operation from being performed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;NoSuchCookieException&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AdZJGAdaT42dr5clj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AdZJGAdaT42dr5clj.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This exception will be raised if a cookie with the specified path name is not contained amongst the corresponding cookies of an active document in the current browsing session. To learn more about cookies in Selenium, check out this detailed tutorial on &lt;a href="https://www.lambdatest.com/blog/handling-cookies-in-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;handling cookies in the Selenium WebDriver&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Watch this video to understand how you can handle Cookies and perform different operations like deleting, getting the parameter values, and adding them to Selenium WebDriver using Java.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/WaNO9RTfEQw"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;InvalidArgumentException&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AIY18vWKCX88qylaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AIY18vWKCX88qylaw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A command with improper or incorrect argument inputs will raise an InvalidArgumentException.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;InvalidElementStateException&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AmZI6rp9g37qsYZUw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AmZI6rp9g37qsYZUw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This exception is raised when we try to perform an operation that does not apply to an element. For example, if we perform a click operation on a radio button that is disabled, the exception will be raised, and hence the command given cannot be executed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;NoSuchWindowException&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2APO3vMRGQmulSlf3p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2APO3vMRGQmulSlf3p.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This exception is thrown when the intended window target cannot be found.&lt;/p&gt;

&lt;p&gt;If you are a Python programmer looking to make a mark, you can take your career to the next level with the Selenium Python 101 certification from LambdaTest.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium Python 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/KeSWHW4Lx1E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Try an &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium online&lt;/a&gt; Grid to run your browser automation testing scripts.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Errors and Exceptions in Selenium Python
&lt;/h2&gt;

&lt;p&gt;Handling errors and exceptions in Selenium Python are vital when writing production-ready code, since exceptions and errors can arise for different reasons, as discussed earlier.&lt;/p&gt;

&lt;p&gt;A prerequisite to writing tests using Selenium Python is to install Python and Selenium on your machine. When using Python for Selenium testing, the default framework used is PyUnit, also known as unittest, which is influenced by JUnit. It functions similarly to other &lt;a href="https://www.lambdatest.com/blog/11-best-unit-testing-frameworks-for-selenium-automation/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;unit testing frameworks&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;However, in this blog on handling errors and exceptions in Selenium Python, we will use &lt;strong&gt;Pytest&lt;/strong&gt;, another &lt;a href="https://www.lambdatest.com/blog/top-python-frameworks-for-automation/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Python testing framework&lt;/a&gt; that supports functions, APIs, and unit testing. It is compatible with the current versions of Python and is easy to configure using the Python Package Manager (pip) command in the terminal.&lt;/p&gt;

&lt;p&gt;If you are new to testing using Selenium Python, here is a detailed step-by-step tutorial on &lt;a href="https://www.lambdatest.com/blog/getting-started-with-selenium-python/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;how to get started with Selenium Python&lt;/a&gt;. You will learn how to configure Python, Selenium, and Pytest on your machine to start automation testing with Selenium. You can learn more about Pytest through this &lt;a href="https://www.lambdatest.com/learning-hub/selenium-pytest-tutorial?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Pytest tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/UzkuOACmBpA"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;For the test project setup, we are using the Visual Studio (VS) Code IDE to write the test scripts. Alternatively, there are other IDEs available, such as Pycharm, Atom, and Sublime Text, that you can use as well.&lt;/p&gt;

&lt;p&gt;For the package manager, we will use Poetry, a Python tool that manages and packages the necessary dependencies in a specific virtual environment for your project. You can declare the libraries your project depends on, and it will take care of managing (installing and updating) them.&lt;/p&gt;

&lt;p&gt;1- To install Poetry on your local machine’s terminal, please use the following command:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;For Windows&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;pip install poetry&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;For Mac&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;pip3 install poetry(depends on the Python version you are using).&lt;/p&gt;

&lt;p&gt;2- Move to the directory where your project will be contained.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AMhgYnUxrgsdss5T5mJzNNQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AMhgYnUxrgsdss5T5mJzNNQ.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3- To initialize Poetry in the terminal, we use the command &lt;strong&gt;poetry init&lt;/strong&gt;. This command will generate a pyproject.toml configuration file containing the build system requirements for Python projects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AXqHC47e06wqvyPyB-rxe3A.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AXqHC47e06wqvyPyB-rxe3A.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4- We need the following libraries in our project to run Selenium Python tests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;selenium (v 4.3.0)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;flake8 (v 4.0.1)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;pytest (v 7.1.2)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;pytest-xdist (v 2.5.0)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;autopep8 (v 1.6.0)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The pytest-xdist plugin adds new test execution modes to Pytest, allowing us to run &lt;a href="https://www.lambdatest.com/blog/pytest-tutorial-parallel-testing-with-selenium-grid/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;parallel Selenium Python tests&lt;/a&gt;. Autopep8 formats the Python code to adhere to the PEP 8 style manual. Flake8 is a great tool that tests your code for errors against the PEP 8 style guide.&lt;/p&gt;

&lt;p&gt;5- Adding Selenium:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ATivgaBtglHayOUpJq-quBA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ATivgaBtglHayOUpJq-quBA.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;6- Adding pytest:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A8-lMBEDvduDCzR7jNsGrWw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A8-lMBEDvduDCzR7jNsGrWw.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;7- Adding pytest-xdist.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AV1NTWMOgVZeqC09IfVkibA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AV1NTWMOgVZeqC09IfVkibA.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;8- Adding autopep8.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A21kMntTJyQRFvKNB8tgXxA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A21kMntTJyQRFvKNB8tgXxA.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;9- Adding flake8.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AUzoScI9zYp9wNR4d6QWAyA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AUzoScI9zYp9wNR4d6QWAyA.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;10- After installing the required dependencies, continue to press &lt;strong&gt;Enter&lt;/strong&gt;, then confirm the generation of the file.&lt;/p&gt;

&lt;p&gt;11- The &lt;em&gt;pyproject.toml&lt;/em&gt; file contains all the packages that must be installed in the virtual environment.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[tool.poetry]
name = "tests"
version = "0.1.0"
description = ""
authors = ["Eugene Kwaka &amp;lt;eugenekwaka@gmail.com&amp;gt;"]

[tool.poetry.dependencies]
python = "^3.7"
selenium = "^4.3.0"
pytest = "^7.1.2"
pytest-xdist = "^2.5.0"
autopep8 = "^1.6.0"
flake8 = "^4.0.1"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core&amp;gt;=1.0.0"]
build-backend = "poetry.core.masonry.api"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To install the dependencies mentioned, run the following command to create the &lt;em&gt;pyproject.toml&lt;/em&gt; and poetry.lock files in the directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsISdX1QtnRGoRFW2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsISdX1QtnRGoRFW2.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A virtual environment (venv) containing the dependencies installed will be automatically created by Poetry. This is important since we do not want to mix Python’s global dependencies with the specific dependencies we require to run the tests in the project.&lt;/p&gt;

&lt;p&gt;In our project, we create a folder that will hold the test functions. Let’s call it “&lt;em&gt;seltests&lt;/em&gt;”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A_Y3gajYJSt5kaXAx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A_Y3gajYJSt5kaXAx.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Perform manual or automated cross browser testing on 3000+ browsers online. Deploy and scale faster with the most powerful cross &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;browser test&lt;/a&gt; tool online.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Errors and Exceptions on the Cloud Selenium Grid
&lt;/h2&gt;

&lt;p&gt;Now that we have covered the essentials to get started with &lt;a href="https://www.lambdatest.com/python-web-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Python web automation&lt;/a&gt; in Selenium, we will look at handling errors and exceptions in Selenium Python on a cloud grid. In this instance, we are using LambdaTest.&lt;/p&gt;

&lt;p&gt;LambdaTest is a cloud-based &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;cross browser testing&lt;/a&gt; platform that allows you to perform &lt;a href="https://www.lambdatest.com/python-automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Python automation testing&lt;/a&gt; at scale over a &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;cloud Selenium Grid&lt;/a&gt;. The unique and fully featured platform offers an &lt;a href="https://www.lambdatest.com/online-browser-farm?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;online browser farm&lt;/a&gt; of 3000+ real browsers across multiple operating systems; thus helping businesses deliver more quality software at a more rapid speed.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/WZlsHlReRww"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You can also Subscribe to the &lt;a href="https://www.youtube.com/channel/UCCymWVaTozpEng_ep0mdUyw?sub_confirmation=1?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest YouTube Channel&lt;/a&gt; and stay updated with the latest tutorials around &lt;a href="https://www.lambdatest.com/automated-browser-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automated browser testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/cypress-e2e-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Cypress E2E testing&lt;/a&gt;, CI/CD, and more.&lt;/p&gt;

&lt;p&gt;To get started with LambdaTest, please follow the below-mentioned steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Register and log in to your LambdaTest account.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Move to the &lt;a href="https://accounts.lambdatest.com/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Profile Section&lt;/a&gt; and copy your &lt;strong&gt;username&lt;/strong&gt; and &lt;strong&gt;access key&lt;/strong&gt;, which will be used to access the Remote Selenium Grid of LambdaTest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You will also need the &lt;strong&gt;desired capabilities&lt;/strong&gt; generated using the &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Capabilities Generator&lt;/a&gt;. You can generate the capabilities needed for the specific test you are carrying out on that page.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have listed the different types of exceptions in Selenium Python in the blog. Now, we will dig deeper into two common exceptions in Selenium Python when performing automated tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  NoSuchElementException
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;NoSuchElementException&lt;/strong&gt; is thrown if we attempt to locate any element unavailable on the page (or the DOM).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario 1 (Chrome Browser)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to the URL link &lt;a href="https://www.lambdatest.com/selenium-playground/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Locate an element called “p-10” by its name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the element.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Scenario 2 (Firefox Browser)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to the URL link &lt;a href="https://www.lambdatest.com/selenium-playground/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Locate an element called “level-up” by its name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the element.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Scenario 1 on the Chrome Browser):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;FileName — test_nosuchelement_google.py&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pytest
from selenium import webdriver
import sys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException


# Desired Capabilities according to SELENIUM 4
ch_capabilities = {
  'LT:Options' : {
   "user" : "&amp;lt;username&amp;gt;",
   "accessKey" : "&amp;lt;accesskey&amp;gt;",
   "build" : "NoSuchElementException Test on Chrome",
   "name" : "NoSuchElementException Test on Chrome",
   "platformName" : "Windows 10"
  },
  "browserName" : "Chrome",
  "browserVersion" : "102.0",
 }


def test_lambdatest_nosuchelement():
    # LambdaTest Profile username
    user_name = "&amp;lt;username&amp;gt;"
    # LambdaTest Profile access_key
    app_key = "&amp;lt;accesskey&amp;gt;"
    # Remote Url to connect to our instance of LambdaTest
    remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"
    ch_driver = webdriver.Remote(
        command_executor=remote_url, desired_capabilities = ch_capabilities)
    ch_driver.get('https://www.lambdatest.com/selenium-playground/')
    ch_driver.maximize_window()
    # will find an element by its NAME property on the page and click it
    ch_driver.find_element(By.CLASS_NAME, "p-10").click()
    ch_driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Scenario 2 on the Firefox Browser):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;FileName — test_nosuchelement_firefox.py&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pytest
from selenium import webdriver
import sys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# Desired Capabilities according to SELENIUM 4
ff_capabilities = {
        'LT:Options' : {
            "user" : "&amp;lt;username&amp;gt;",
            "accessKey" : "&amp;lt;access_key&amp;gt;",
            "build" : "NoSuchElementException Test for LambdaTest (Firefox)",
            "name" : "NoSuchElementException Test for LambdaTest (Firefox)",
            "platformName" : "Windows 10"
        },
        "browserName" : "Firefox",
        "browserVersion" : "101.0",
    }

def test_lambdatest_firefox():
    # LambdaTest Profile username
    user_name = "&amp;lt;username&amp;gt;"
    # LambdaTest Profile access_key
    app_key = "&amp;lt;access_key&amp;gt;"
    # Remote Url to connect to our instance of LambdaTest
    remote_url = "https://" + user_name + ":" + app_key +   "@hub.lambdatest.com/wd/hub"
    # creating an instance of Firefox based on the remote url and the desired capabilities
    ff_driver = webdriver.Remote(
        command_executor=remote_url, desired_capabilities = ff_capabilities)
    ff_driver.get('https://www.lambdatest.com/selenium-playground/')
    # This will maximize the window interface of the driver class in this case it's FIREFOX
    ff_driver.maximize_window()
    # Will find an element by its NAME property in the page and click it
    ff_driver.find_element(By.CLASS_NAME, "level-up").click()
    ff_driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We run the two test scripts using &lt;a href="https://www.lambdatest.com/blog/what-is-parallel-testing-and-why-to-adopt-it/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;parallel testing&lt;/a&gt;, which allows the execution of the same tests simultaneously. To run the test, type the following command in the terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry run pytest seltests/test_nosuchelement_google.py seltests/test_nosuchelement_firefox.py -n 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The terminal shows that the test execution failed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2318%2F0%2Arob1QICYvAOaw5A1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2318%2F0%2Arob1QICYvAOaw5A1.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the LambdaTest Builds dashboard, the failed test executions are displayed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2AlAaeoD_JvifzC6cd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2AlAaeoD_JvifzC6cd.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We are conducting a joint code walkthrough because the imported Selenium Python libraries used in both test scenarios are identical.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Import the necessary Selenium Python classes — pytest, webdriver, sys, By, NoSuchElementException.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pytest
from selenium import webdriver
import sys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We imported &lt;strong&gt;pytest&lt;/strong&gt;, primarily used for unit testing in &lt;a href="https://www.lambdatest.com/blog/selenium-webdriver-tutorial-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Python&lt;/a&gt;. &lt;a href="https://www.lambdatest.com/selenium-automation-testing-with-wd-framework?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt; is a web framework that enables us to perform cross-browser testing, and in this case, we are using Python to write the test scripts. The &lt;strong&gt;By&lt;/strong&gt; query is based on the &lt;a href="https://www.lambdatest.com/blog/how-to-find-element-by-text-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;find_element() method in Selenium&lt;/a&gt; that targets a specific locator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: We specify the &lt;strong&gt;browser capabilities&lt;/strong&gt; based on the browser we use to conduct the automated tests in LambdaTest. They are generated using the LambdaTest Capabilities Generator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ch_capabilities = {
        'LT:Options' : {
            "user" : "&amp;lt;username&amp;gt;",
            "accessKey" : "&amp;lt;accesskey&amp;gt;",
            "build" : "NoSuchElementException Test on Chrome",
            "name" : "NoSuchElementException Test on Chrome",
            "platformName" : "Windows 10"
        },
        "browserName" : "Chrome",
        "browserVersion" : "102.0",
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Firefox&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ff_capabilities = {
        'LT:Options' : {
            "user" : "&amp;lt;username&amp;gt;",
            "accessKey" : "&amp;lt;accesskey&amp;gt;",
            "build" : "NoSuchElementException Test for LambdaTest (Firefox)",
            "name" : "NoSuchElementException Test for LambdaTest (Firefox)",
            "platformName" : "Windows 10"
        },
        "browserName" : "Firefox",
        "browserVersion" : "101.0",
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: We define a function that starts with “test” in which we assign the test methods. You can learn more about it through this blog on &lt;a href="https://www.lambdatest.com/blog/selenium-webdriver-with-python/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Pytest testing using Selenium WebDriver&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def test_lambdatest_nosuchelement():
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: We then set our LambdaTest &lt;strong&gt;username&lt;/strong&gt; and &lt;strong&gt;accesskey&lt;/strong&gt; to variables we appended to &lt;strong&gt;remote_url&lt;/strong&gt;. This remote URL connects us to the Remote Selenium Grid (&lt;a class="mentioned-user" href="https://dev.to/hub"&gt;@hub&lt;/a&gt;.lambdatest.com/wd/hub).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# LambdaTest Profile username
    user_name = "&amp;lt;username&amp;gt;"
    # LambdaTest Profile access_key
    app_key = "&amp;lt;accesskey&amp;gt;"
    # Remote Url to connect to our instance of LambdaTest
    remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: We then use the &lt;em&gt;remote_url&lt;/em&gt; and browser capabilities to instantiate the corresponding browser (i.e. Chrome or Firefox)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ch_driver = webdriver.Remote(
        command_executor=remote_url, desired_capabilities = ch_capabilities)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Firefox&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ff_driver = webdriver.Remote(
        command_executor=remote_url, desired_capabilities = ff_capabilities)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;: We then get the URL of the website on which we want to locate a specific element. In our test, we try to locate an element by its name and click on it. Once the element had been located and clicked, we quit the test.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ch_driver.get('https://www.lambdatest.com/selenium-playground/')
    ch_driver.maximize_window()
    # Will find an element by its NAME property in the page and click it
    ch_driver.find_element(By.CLASS_NAME, "p-10").click()
    ch_driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Firefox&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ff_driver.get('https://www.lambdatest.com/selenium-playground/')
    # This will maximize the window interface of the driver class in this case it's FIREFOX
    ff_driver.maximize_window()
    # Will find an element by its NAME property in the page and click it
    ff_driver.find_element(By.CLASS_NAME, "p-10").click()
    ff_driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Watch this video to learn exception handling in Selenium, which covers different types of exceptions and how to handle them using various methods.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/KyFnIxW-CF4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Need a great solution for cross browser testing on Safari? Forget about emulators or simulators — use real online browsers. Try LambdaTest to test on &lt;a href="https://www.lambdatest.com/test-on-safari-browsers?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Safari browser online&lt;/a&gt;!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  NoSuchElementException Handling in Action
&lt;/h3&gt;

&lt;p&gt;According to the exception mentioned above, we must catch “NoSuchElementException” and print the proper log messages to guarantee that our test fails with the proper error message.&lt;/p&gt;

&lt;p&gt;We use the &lt;strong&gt;try&lt;/strong&gt; and &lt;strong&gt;except&lt;/strong&gt; method to catch the exception when it is raised in the test script. The “try” block lets you check the code for errors, while the “except” block lets you handle the errors or exceptions raised.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pytest
from selenium import webdriver
import sys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

ch_capabilities = {
        'LT:Options' : {
            "user" : "&amp;lt;username&amp;gt;",
            "accessKey" : "&amp;lt;accesskey&amp;gt;",
            "build" : "NoSuchElementException Test on Chrome",
            "name" : "NoSuchElementException Test on Chrome",
            "platformName" : "Windows 10"
        },
        "browserName" : "Chrome",
        "browserVersion" : "102.0",
    }

def test_lambdatest_nosuchelement():
    # LambdaTest Profile username
    user_name = "&amp;lt;username&amp;gt;"
    # LambdaTest Profile access_key
    app_key = "&amp;lt;accesskey&amp;gt;"
    # Remote Url to connect to our instance of LambdaTest
    remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"
    ch_driver = webdriver.Remote(
        command_executor=remote_url, desired_capabilities = ch_capabilities)
    ch_driver.get('https://www.lambdatest.com/selenium-playground/')
    ch_driver.maximize_window()
    try:
    # 'By' is used to locate the element by its property
        ch_driver.find_element(By.CLASS_NAME, "p-10")
        ch_driver.click()
    except NoSuchElementException:
        print("Exception Handled")

    ch_driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Firefox&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pytest
from selenium import webdriver
import sys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# Desired Capabilities according to SELENIUM 4
ff_capabilities = {
        'LT:Options' : {
            "user" : "&amp;lt;username&amp;gt;",
            "accessKey" : "&amp;lt;accesskey&amp;gt;",
            "build" : "NoSuchElementException Test for LambdaTest (Firefox)",
            "name" : "NoSuchElementException Test for LambdaTest (Firefox)",
            "platformName" : "Windows 10"
        },
        "browserName" : "Firefox",
        "browserVersion" : "101.0",
    }

def test_lambdatest_nosuchelement():
    # LambdaTest Profile username
    user_name = "&amp;lt;username&amp;gt;"
    # LambdaTest Profile access_key
    app_key = "&amp;lt;accesskey&amp;gt;"
    # Remote Url to connect to our instance of LambdaTest
    remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"
    # creating an instance of Firefox based on the remote url and the desired capabilities
    ff_driver = webdriver.Remote(
        command_executor=remote_url, desired_capabilities = ff_capabilities)
    ff_driver.get('https://www.lambdatest.com/selenium-playground/')
    # This will maximize the window interface of the driver class in this case it's FIREFOX
    ff_driver.maximize_window()
    try:
        # Will find an element by its NAME property in the page and click it
        ff_driver.find_element(By.CLASS_NAME, "p-10").click()
    except NoSuchElementException:
        print("Exception Handled")
    ff_driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use the terminal to enter the following command to perform &lt;a href="https://www.lambdatest.com/selenium-python-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Python testing&lt;/a&gt; in parallel on the LambdaTest Grid.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry run pytest seltests/test_handle_nosuchelement_google.py 
seltests/test_handle_nosuchelement_firefox.py -n 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The tests will be executed successfully.&lt;/p&gt;

&lt;p&gt;The passed tests will be shown as follows in the LambdaTest Dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2436%2F0%2A_YGEZ2g8u6v8sPTj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2436%2F0%2A_YGEZ2g8u6v8sPTj.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The passed tests will be shown as follows in the &lt;a href="https://accounts.lambdatest.com/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2A1r5N-ela4MhJtoyS.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2A1r5N-ela4MhJtoyS.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The “try” block will run the &lt;strong&gt;find_element()&lt;/strong&gt; argument and take in the &lt;strong&gt;By&lt;/strong&gt; query to locate an element using its name in the URL provided.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: In the browser’s inspect tool, click on &lt;strong&gt;Console&lt;/strong&gt; and type the following command to find the element with the name &lt;em&gt;“p-10”&lt;/em&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$x ("//*[text()='p-10']")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2AcCRC4NBiY4BjkCs9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2AcCRC4NBiY4BjkCs9.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The test does not fail since the element with the name “p-10” does not exist. The “except” block will run, catching the exception.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; The try and except code block is the same in Chrome and Firefox text scripts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2652%2F0%2ALKeOOvhEpowFESuo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2652%2F0%2ALKeOOvhEpowFESuo.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Firefox&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  StaleElementReferenceException
&lt;/h2&gt;

&lt;p&gt;This exception is raised whenever an element is not present in the DOM or is deleted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario 1 (Chrome Browser)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to the URL link &lt;a href="https://ecommerce-playground.lambdatest.io/index.php?route=account/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;https://ecommerce-playground.lambdatest.io/index.php?route=account/login&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find the reference to the email and password text fields.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on a link that reloads the entire web page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter the password into the password field with the reference we located before the web page reloaded.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Scenario 2 (Firefox Browser)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to the URL link &lt;a href="https://ecommerce-playground.lambdatest.io/index.php?route=account/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;https://ecommerce-playground.lambdatest.io/index.php?route=account/login&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find the reference to the email and password text fields.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on a link that reloads the entire web page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter the password into the password field with the reference we located before the web page reloaded.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Scenario 1 on the Chrome Browser):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;FileName — test_staleelement_chrome.py&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pytest
from selenium import webdriver
import sys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import StaleElementReferenceException


ch_capabilities = {
        'LT:Options' : {
            "user" : "&amp;lt;username&amp;gt;",
            "accessKey" : "&amp;lt;accesskey&amp;gt;",
            "build" : "StaleElementReferenceException Test on Chrome",
            "name" : "StaleElementReferenceException Test on Chrome",
            "platformName" : "Windows 10"
        },
        "browserName" : "Chrome",
        "browserVersion" : "102.0",
    }

def test_ecommerceplayground_staleelement():
     # LambdaTest Profile username
    user_name = "&amp;lt;username&amp;gt;"
    # LambdaTest Profile access_key
    app_key = "&amp;lt;accesskey&amp;gt;"
    # Remote Url to connect to our instance of LambdaTest
    remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"
    # creating an instance of Firefox based on the remote url and the desired capabilities
    ch_driver = webdriver.Remote(
        command_executor=remote_url, desired_capabilities = ch_capabilities)
    ch_driver.get('https://ecommerce-playground.lambdatest.io/index.php?route=account/login')

    emailElement = ch_driver.find_element(By.ID, "input-email")
    passwordElement = ch_driver.find_element(By.ID, "input-password")

    emailElement.send_keys("email@gmail.com")

    ch_driver.find_element(By.XPATH, "//input[@type='submit']").click()

    passwordElement.send_keys("password")

    ch_driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Scenario 2 on the Firefox Browser):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;FileName — test_staleelement_firefox.py&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pytest
from selenium import webdriver
import sys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import StaleElementReferenceException

# Desired Capabilities according to SELENIUM 4
ff_capabilities = {
        'LT:Options' : {
            "user" : "&amp;lt;username&amp;gt;",
            "accessKey" : "&amp;lt;accesskey&amp;gt;",
            "build" : "StaleElementReferenceException Test for LambdaTest (Firefox)",
            "name" : "StaleElementReferenceException Test for LambdaTest (Firefox)",
            "platformName" : "Windows 10"
        },
        "browserName" : "Firefox",
        "browserVersion" : "101.0",
    }


def test_ecommerceplayground_staleelement():
     # LambdaTest Profile username
    user_name = "&amp;lt;username&amp;gt;"
    # LambdaTest Profile access_key
    app_key = "&amp;lt;accesskey&amp;gt;"
    # Remote Url to connect to our instance of LambdaTest
    remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"
    # creating an instance of Firefox based on the remote url and the desired capabilities
    ff_driver = webdriver.Remote(
        command_executor=remote_url, desired_capabilities = ff_capabilities)
    ff_driver.get('https://ecommerce-playground.lambdatest.io/index.php?route=account/login')

    emailElement = ff_driver.find_element(By.ID, "input-email")
    passwordElement = ff_driver.find_element(By.ID, "input-password")

    emailElement.send_keys("email@gmail.com")

    ff_driver.find_element(By.XPATH, "//input[@type='submit']").click()

    passwordElement.send_keys("password")

    ff_driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run the following command in the terminal to execute the test scripts in parallel testing.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry run pytest seltests/test_staleelement_google.py  seltests/test_staleelement_firefox.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We receive a StaleElementExceptionError message in the terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2502%2F0%2ASvE89MIo4lGB_lfy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2502%2F0%2ASvE89MIo4lGB_lfy.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can view the raised exceptions in the LambdaTest Dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2AnNj3Fz47hhBvL--B.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2AnNj3Fz47hhBvL--B.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Import the necessary Selenium Python classes — pytest, webdriver, sys, By, and StaleElementReferenceException.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pytest
from selenium import webdriver
import sys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import StaleElementReferenceException
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; We generate capabilities in the LambdaTest Capabilities based on the browser and OS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ch_capabilities = {
    'LT:Options': {
        "user": "&amp;lt;username&amp;gt;",
        "accessKey": "&amp;lt;accesskey&amp;gt;",
        "build": "StaleElementReferenceException Test on Chrome",
        "name": "StaleElementReferenceException Test on Chrome",
        "platformName": "Windows 10"
    },
    "browserName": "Chrome",
    "browserVersion": "102.0",
}

&amp;lt;pre&amp;gt;

&amp;lt;center&amp;gt;&amp;lt;strong&amp;gt;Firefox&amp;lt;/strong&amp;gt;&amp;lt;/center&amp;gt;

&amp;lt;pre&amp;gt;ff_capabilities = {
        'LT:Options' : {
            "user" : "&amp;lt;username&amp;gt;",
            "accessKey" : "&amp;lt;accesskey&amp;gt;",
            "build" : "StaleElementReferenceException Test for LambdaTest (Firefox)",
            "name" : "StaleElementReferenceException Test for LambdaTest (Firefox)",
            "platformName" : "Windows 10"
        },
        "browserName" : "Firefox",
        "browserVersion" : "101.0",
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; We create a function that will contain our test methods. In the function, we define the username and password for a remote_url that will connect to the Remote Selenium Grid.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def test_ecommerceplayground_staleelement():
    # LambdaTest Profile username
    user_name = "&amp;lt;username&amp;gt;"
    # LambdaTest Profile access_key
    app_key = "&amp;lt;accesskey&amp;gt;"
    # Remote Url to connect to our instance of LambdaTest
    remote_url = "https://" + user_name + ":" + \
        app_key + "@hub.lambdatest.com/wd/hub"
    # creating an instance of Chrome based on the remote url and the desired capabilities
    ch_driver = webdriver.Remote(
        command_executor=remote_url, desired_capabilities=ch_capabilities)
    ch_driver.get(
     'https://ecommerce-playground.lambdatest.io/index.php?route=account/login')

    emailElement = ch_driver.find_element(By.ID, "input-email")
    passwordElement = ch_driver.find_element(By.ID, "input-password")

    emailElement.send_keys("email@gmail.com")

    ch_driver.find_element(By.XPATH, "//input[@type='submit']").click()

    passwordElement.send_keys("password")

    ch_driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code for the browser tests is the same. We set the Chrome webdriver to &lt;strong&gt;ch_driver&lt;/strong&gt; while the Firefox webdriver is &lt;strong&gt;ff_driver&lt;/strong&gt;. Based on their ids, the emailElement variable will locate the email input element, while the &lt;strong&gt;passwordElement&lt;/strong&gt; variable will locate the input element.&lt;/p&gt;

&lt;p&gt;We then enter an email into the email input using the &lt;strong&gt;emailElement.send_keys()&lt;/strong&gt; function and locate and click the &lt;strong&gt;login&lt;/strong&gt; button, which is the type “submit”. Then enter the password and finally quit the test with the &lt;strong&gt;.quit() function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2Aice8seIFFv9dUgkU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2Aice8seIFFv9dUgkU.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the code is run, a StaleElementReferenceException will be raised for the following reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In Chrome, the element is not attached to the page document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In Firefox, the element reference password is no longer attached to the DOM.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We apply the &lt;strong&gt;try&lt;/strong&gt; and &lt;strong&gt;except&lt;/strong&gt; code blocks to handle the “StaleElementReferenceException” when it is thrown during our test’s execution.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pytest
from selenium import webdriver
import sys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import StaleElementReferenceException

ch_capabilities = {
        'LT:Options' : {
            "user" : "&amp;lt;username&amp;gt;",
            "accessKey" : "&amp;lt;accesskey&amp;gt;",
            "build" : "Errors and Exceptions Test on Chrome",
            "name" : "Errors and Exceptions Test on Chrome",
            "platformName" : "Windows 10"
        },
        "browserName" : "Chrome",
        "browserVersion" : "102.0",
    }


def test_ecommerceplayground_staleelement():
     # LambdaTest Profile username
    user_name = "&amp;lt;username&amp;gt;"
    # LambdaTest Profile access_key
    app_key = "&amp;lt;accesskey&amp;gt;"
    # Remote Url to connect to our instance of LambdaTest
    remote_url = "https://" + user_name + ":" + app_key + "@hub.lambdatest.com/wd/hub"
    # creating an instance of Firefox based on the remote url and the desired capabilities
    ch_driver = webdriver.Remote(
        command_executor=remote_url, desired_capabilities = ch_capabilities)
    ch_driver.get('https://ecommerce-playground.lambdatest.io/index.php?route=account/login')

    emailElement = ch_driver.find_element(By.ID, "input-email")
    passwordElement = ch_driver.find_element(By.ID, "input-password")

    emailElement.send_keys("email@gmail.com")

    ch_driver.find_element(By.XPATH, "//input[@type='submit']").click()

    try:
        passwordElement.send_keys("password")

    except StaleElementReferenceException:
        print('StaleElementReferenceException handled')
        passwordElement = ch_driver.find_element(By.ID, "input-password")
        passwordElement.send_keys("password")

    ch_driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The try block will run the code to add the password in which the exception will be raised. The except block will handle the exception by locating the password input element and sending the password value.&lt;/p&gt;

&lt;p&gt;When the code is run, it will execute successfully in the terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry run pytest seltests/test_handle_staleelement_google.py seltests/test_handle_staleelement_firefox.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2476%2F0%2ArVYj92jKaEXcv9yd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2476%2F0%2ArVYj92jKaEXcv9yd.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see successful test executions in the LambdaTest Build dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2AtRAN8-QoE0iU2E1S.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2AtRAN8-QoE0iU2E1S.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For a complete view of all test results and trends, check out your &lt;strong&gt;&lt;a href="https://analytics.lambdatest.com/test-overview?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Analytics Dashboard&lt;/a&gt;&lt;/strong&gt;. It lets you see how the tests are moving along, so you can make sure that everything is executed accordingly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AnBz4yUC8NbfwQ_r0.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AnBz4yUC8NbfwQ_r0.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your native, hybrid, and web apps across all legacy and latest mobile operating systems on the most powerful &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec01_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Online emulator Android&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog, we have looked at some common exceptions in Selenium Python. We have also deep-dived into various exceptions and looked at why &lt;strong&gt;NoSuchElementException&lt;/strong&gt; and &lt;strong&gt;StaleElementReference&lt;/strong&gt; are generated and how to handle them. Selenium Python provides methods using which you can handle various exceptions, and in this case, we have focused on the &lt;strong&gt;try&lt;/strong&gt; and &lt;strong&gt;except&lt;/strong&gt; method.&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How To Handle Multiple Windows In Selenium Python</title>
      <dc:creator>EugeneKwaka</dc:creator>
      <pubDate>Wed, 30 Nov 2022 14:15:46 +0000</pubDate>
      <link>https://forem.com/testmuai/how-to-handle-multiple-windows-in-selenium-python-30di</link>
      <guid>https://forem.com/testmuai/how-to-handle-multiple-windows-in-selenium-python-30di</guid>
      <description>&lt;p&gt;Automating testing is a crucial step in the development pipeline of a software product. In an agile development environment, where there is continuous development, deployment, and maintenance of software products, &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt; ensures that the end software products delivered are error-free.&lt;/p&gt;

&lt;p&gt;In the world of automation, Selenium is one of the most widely used open-source &lt;a href="https://www.lambdatest.com/blog/best-test-automation-frameworks-2021/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automation testing frameworks&lt;/a&gt; to execute automated web browser tests on websites. Selenium can run automated tests on various browsers (e.g., Chrome, Firefox, Edge, etc.). The test automation scripts can be devised using popular frameworks offered by languages like C#, Java, JavaScript, Python, and more.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb2w82i7xauhndnkvg8dg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb2w82i7xauhndnkvg8dg.png" alt="**Selenium WebDriver Architecture**" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The automated test cases you perform using Selenium might lead you to instances where it is necessary to handle multiple windows while working with a web application. This situation may arise as a result of clicking a button on the website that opens up a URL to a new tab or a new window. For example, the job search platform Glassdoor opens a new window when you click the buttons to sign in using your Google account or Facebook account.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwen7g4be432bnz8m46hj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwen7g4be432bnz8m46hj.png" alt="**Glassdoor Home Page**" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the &lt;strong&gt;“Continue with Facebook”&lt;/strong&gt; button to sign in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgqlzi9kr2kwx5lb1ro01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgqlzi9kr2kwx5lb1ro01.png" alt="**Facebook Page Window**" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A Facebook webpage window will pop up to prompt you to sign in.&lt;/p&gt;

&lt;p&gt;As a result, the user might need to access the open browser window to move forward.&lt;/p&gt;

&lt;p&gt;As a QA tester, running a test case might require you to complete some operations in a newly opened browser window or tab before going back to the original window or tab to finish the rest of the tasks.&lt;/p&gt;

&lt;p&gt;In this blog on how to handle multiple windows in Selenium Python, we will discuss the basics of browser windows and how to handle multiple windows in Selenium Python. By the end of the blog, you will be able to handle multiple windows when they arise when executing test cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Learn what Selenium is and its benefits for automated cross browser testing. See how &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; works and get ready to use it for testing. Read more.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Browser Window in Selenium?
&lt;/h2&gt;

&lt;p&gt;A browser window, also known as the main window or parent window, is the handle of the homepage or current open webpage that a user lands on after accessing a browser. Upon running a Selenium automation script, the parent window will launch. Creating a &lt;a href="https://www.lambdatest.com/blog/selenium-webdriver-tutorial-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt; session opens up a window that is, by default, under the WebDriver’s control.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9an85tgspc2eounitanb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9an85tgspc2eounitanb.jpg" alt="***Chrome browser window is controlled by automated test software***" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To have a view of what a parent window looks like, here is a window of the &lt;a href="https://www.lambdatest.com/selenium-playground/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Selenium Playground website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fckd26osb83c2so4kcnn6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fckd26osb83c2so4kcnn6.jpg" alt="***Selenium Playground Parent Window***" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/selenium-automation-testing-with-wd-framework?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt; always keeps the context of the parent window when navigating to a defined URL that opens a new window. The child window will be within the context of the parent window. This enables navigating back and forth between the windows once you are done with the operations you were handling in the newly opened child window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mmv380y37hc0yq3fkka.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mmv380y37hc0yq3fkka.png" alt="***The Hierarchy of Browser Windows***" width="800" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was all about the browser window in Selenium. In the next section of this blog on how to handle multiple windows in Selenium Python, we will learn more about child windows in Selenium.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Are you using Playwright for automation testing? Run your &lt;a href="https://www.lambdatest.com/playwright-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Playwright&lt;/a&gt; test scripts instantly on 50+ browser/OS combinations using the LambdaTest cloud. Sign up for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Child Window in Selenium?
&lt;/h2&gt;

&lt;p&gt;When we click on a button or URL link in the parent window, and the action opens another window(s) within the main window, the new window(s) is called a &lt;strong&gt;child window&lt;/strong&gt;. The new window(s) or tab(s) opened are called children of the parent window and have unique window handles.&lt;/p&gt;

&lt;p&gt;To demonstrate the handling of child windows, let’s use the &lt;a href="https://www.lambdatest.com/selenium-playground/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Selenium Playground&lt;/a&gt; we mentioned earlier:&lt;/p&gt;

&lt;p&gt;1- Click on the &lt;a href="https://www.lambdatest.com/selenium-playground/window-popup-modal-demo?utm_source=detvo&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Windows Popup Modal&lt;/a&gt; link in the main window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxib8l8m5z8zh4some7cn.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxib8l8m5z8zh4some7cn.jpeg" alt="***Click on the Window Popup Modal option***" width="800" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2- Click on the buttons in the Single Window Popup Modal to open a single child window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foe5emesafayoxgv5i633.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foe5emesafayoxgv5i633.jpeg" alt="***Click on the buttons in the Single Window Popup to open the Child Window***" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fip43fa4usbdubixiqsoh.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fip43fa4usbdubixiqsoh.jpeg" alt="***Single Window Popup***" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3- Click on the Multiple Window Popup buttons to open multiple child browser windows. The first button opens two child windows, while the “Follow All” button opens three child windows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fudcdi97u9i04l3s5ggrl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fudcdi97u9i04l3s5ggrl.jpg" alt="***Two Child Windows***" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7vl9h5qr7g7kenthlja.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7vl9h5qr7g7kenthlja.jpg" alt="***Three Child Windows***" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The current page you are on is the main window, while the windows (Facebook, Twitter, and LambdaTest web pages) open by clicking the buttons are the child windows.&lt;/p&gt;

&lt;p&gt;It is possible to open a single window or multiple child windows from a single main window. It is also important to note that a child window may or may not contain a URL link.&lt;/p&gt;

&lt;p&gt;While conducting a test case on the above scenario manually, it is simple to examine the behavior of the child windows because of their visibility in the context of the parent window. However, this is not the case when using Selenium to automate the test case because we need to change the WebDriver’s context from the parent window to the child window to perform actions on it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;A comprehensive &lt;a href="https://www.lambdatest.com/learning-hub/end-to-end-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;End to End testing&lt;/a&gt; tutorial that covers what E2E Testing is, its importance, benefits, and how to perform it with real-time examples.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Window Handle in Selenium Python?
&lt;/h2&gt;

&lt;p&gt;Since we have discussed the concept of windows in Selenium in the above section of this blog on how to handle multiple windows in Selenium Python, let us move on to learning how to control multiple windows.&lt;/p&gt;

&lt;p&gt;Multiple windows are brought up by scenarios in which a web application navigates to or opens multiple browser windows. Selenium provides the option to handle multiple windows with &lt;strong&gt;window handles&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Selenium WebDriver does not distinguish between windows and tabs. It handles both using window handles. Whenever a window is opened, it contains a distinct ID that returns a string value, making it unique and persisting for the entire session.&lt;/p&gt;

&lt;p&gt;Using the &lt;em&gt;Window Popup Modal&lt;/em&gt; Demo page from the LambdaTest Selenium Playground website, we can see the IDs of the main browser window and the child window, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq6xtliiqj3zh8weuc8fm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq6xtliiqj3zh8weuc8fm.png" width="800" height="54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to handle multiple windows in Selenium Python?
&lt;/h2&gt;

&lt;p&gt;We can handle the occurrence of child windows or tabs when running test cases using Selenium Python. When a parent window opens a child window, the WebDriver only controls one window at a time in the running session.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;WebDriver&lt;/a&gt; controls the main window, and any actions in a test script will be executed in the currently active window.&lt;/p&gt;

&lt;p&gt;The Selenium Python WebDriver provides the following methods to handle multiple windows.&lt;/p&gt;

&lt;h2&gt;
  
  
  current_window_handle()
&lt;/h2&gt;

&lt;p&gt;This method collects the unique window handle ID of the browser window that is currently active.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt; &lt;em&gt;driver.current_window_handle&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29xosck8egc2rgka75il.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29xosck8egc2rgka75il.png" width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  window_handles()
&lt;/h2&gt;

&lt;p&gt;This method returns the IDs of all the windows that have been opened by the WebDriver in the current session. It stores the window IDs to be used for switching windows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt; &lt;em&gt;driver.window_handles&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7pcfjfd2s1300fbh8hza.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7pcfjfd2s1300fbh8hza.png" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  switch_to.window()
&lt;/h2&gt;

&lt;p&gt;This method switches the WebDriver’s focus from the currently open browser window to the intended browser window. The targeted window’s id is passed as the argument to shift the WebDriver’s control to the new window.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1:&lt;/strong&gt; driver.switch_to.window()&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F845kumg77w2hauvto4d8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F845kumg77w2hauvto4d8.png" width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 2:&lt;/strong&gt; driver.switch_to.window()&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2gljib0m63bp1bkea273.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2gljib0m63bp1bkea273.png" width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s deep dive into it with a practical demonstration on how to handle multiple windows in Selenium Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Perform browser automation testing on the most powerful cloud infrastructure. Leverage LambdaTest &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Automation testing platform&lt;/a&gt; for faster, reliable and scalable experience on cloud.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: How to handle multiple windows in Selenium Python?
&lt;/h2&gt;

&lt;p&gt;Now that we have looked at the different methods available in Selenium Python to handle multiple windows, this section on how to handle multiple windows in Selenium Python looks at how to execute these Selenium tests on a &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;cloud Selenium Grid&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The best approach to fully utilizing &lt;a href="https://www.lambdatest.com/python-automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Python automation testing&lt;/a&gt; and achieving the intended results quickly is to use a cloud-based Selenium Grid. We can automate multiple window handling in Selenium Python using a cloud-based Selenium Grid platform like LambdaTest.&lt;/p&gt;

&lt;p&gt;LambdaTest enables you to perform automated &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;cross browser testing&lt;/a&gt; at scale using a &lt;a href="https://www.lambdatest.com/selenium-ide-cloud?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium IDE cloud&lt;/a&gt;. The platform offers an &lt;a href="https://www.lambdatest.com/online-browser-farm?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;online browser farm&lt;/a&gt; of 3000+ real web browsers running on various operating systems to ensure that enterprises develop more high-quality software applications faster.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/WZlsHlReRww"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You can also Subscribe to the &lt;a href="https://www.youtube.com/channel/UCCymWVaTozpEng_ep0mdUyw?sub_confirmation=1?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest YouTube Channel&lt;/a&gt; and stay updated with the latest tutorials around &lt;a href="https://www.lambdatest.com/automated-browser-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automated browser testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/cypress-e2e-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Cypress E2E testing&lt;/a&gt;, CI/CD, and more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Accelerate your release velocity with blazing fast &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;test automation cloud&lt;/a&gt;!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;To get started writing tests for &lt;a href="https://www.lambdatest.com/python-web-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Python web automation&lt;/a&gt;, we need to have Python and Selenium installed on the client machine. If you are unfamiliar with Selenium using Python, you can check out this detailed blog on &lt;a href="https://www.lambdatest.com/blog/getting-started-with-selenium-python/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;getting started with Selenium Python&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are many different Python testing frameworks available for Selenium testing, including PyUnit (unittest), pytest, Behave, Robot, Lettuce, Nose2, and Testify. You can learn more about these Selenium Python testing frameworks in detail in this blog post on the &lt;a href="https://www.lambdatest.com/blog/top-python-frameworks-for-automation/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;best Python testing frameworks&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this blog on how to handle multiple windows in Selenium Python, however, we will use the &lt;a href="https://www.lambdatest.com/pytest-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;pytest testing&lt;/a&gt; framework. Pytest is an open-source Python &lt;a href="https://www.lambdatest.com/learning-hub/web-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;web automation&lt;/a&gt; framework mainly used for &lt;a href="https://www.lambdatest.com/learning-hub/unit-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;unit testing&lt;/a&gt; and is scalable by supporting automation tests for functions, APIs, and unit tests.&lt;/p&gt;

&lt;p&gt;This detailed &lt;a href="https://www.lambdatest.com/learning-hub/selenium-pytest-tutorial?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium pytest tutorial&lt;/a&gt; can teach you more about how to configure and begin using the pytest Framework to perform automated tests in Selenium Python.&lt;/p&gt;

&lt;p&gt;We use VS Code(Visual Studio ) IDE to write and run the Selenium Python automation scripts for our test case project. Apart from VS Code, there are other IDEs available such as Atom, PyCharm, and Sublime Text that can be used to write the test scripts as well.&lt;/p&gt;

&lt;p&gt;The package manager we use is PIP, the default package manager for Python. It comes preinstalled when you install Python (version 3.4 and above). Pip allows you to install and manage additional packages that aren’t included in the Python standard library. These packages are built specially to be used for different development frameworks, utilities, and modules.&lt;/p&gt;

&lt;p&gt;The test case project’s demonstration will be run on a Windows operating system. In addition, steps on how to set up the project on a macOS have also been included.&lt;/p&gt;

&lt;p&gt;1- To use Pip to manage our project’s dependencies, let us first look at the version currently available in the command-line terminals&lt;/p&gt;

&lt;p&gt;Windows: &lt;em&gt;pip –version&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxs9dq2zyd48ksp9dlh9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxs9dq2zyd48ksp9dlh9.png" width="800" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;macOS: &lt;em&gt;pip –version&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;2- Create a directory that will hold the test project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuj1co729c44ghs988i73.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuj1co729c44ghs988i73.jpeg" width="751" height="42"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3- Move into the project folder you have just created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fky7nzkuzdo15gzyna813.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fky7nzkuzdo15gzyna813.jpeg" width="699" height="42"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4- Create a virtual environment (venv) that will hold the specific dependencies used by the test project. The creation of a venv is crucial because we do not want to combine the global dependencies provided by Python with the particular libraries we need to run the project’s test cases. We install the virtual environment library on Windows to help create the venv using the following command.&lt;/p&gt;

&lt;p&gt;Windows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsgucq4z8l4d5uil0s8gq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsgucq4z8l4d5uil0s8gq.jpeg" width="713" height="39"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;macOS: &lt;em&gt;sudo pip install virtualenv&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;5- Run the below-mentioned command on the terminal to activate the virtual environment.&lt;/p&gt;

&lt;p&gt;Windows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjwbz3woabeebqyb39ax.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjwbz3woabeebqyb39ax.jpeg" width="799" height="42"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;macOS: &lt;em&gt;virtualenv multiplewindowstest&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;6- Activate the virtual environment so we can install the required libraries to run our test project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiqrx2m2hksuabzbr3y8c.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiqrx2m2hksuabzbr3y8c.jpeg" width="800" height="23"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;macOS: &lt;em&gt;source multiplewindowstest/bin/activate&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;7- We install the following library dependencies in our test project’s venv to run the Selenium Python tests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;selenium (v 4.1.2)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;flake8 (v 4.0.1)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;pytest (v 7.1.2)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;pytest-xdist (v 2.5.0)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;autopep8 (v 1.6.0)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The autopep8 library enforces the PEP 8 style manual on the Python code to ensure it is in the correct format, while flake8 is a library that tests the Python code for errors that are against the PEP 8 style guide.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;pytest-xdist&lt;/em&gt; plugin is an extension of pytest that introduces a new &lt;a href="https://www.lambdatest.com/learning-hub/test-execution?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test execution&lt;/a&gt; mode that enables us to perform &lt;a href="https://www.lambdatest.com/blog/pytest-tutorial-parallel-testing-with-selenium-grid/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;parallel Selenium Python tests&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;1- To install the dependencies described above, write the command below in the terminal as follows.&lt;/p&gt;

&lt;p&gt;Syntax — &lt;strong&gt;pip install [library name]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;2- Create a &lt;em&gt;“requirements.txt”&lt;/em&gt; file that contains all the installed project dependencies in the root folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pip freeze &amp;gt;requirements.txt&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;autopep8==1.6.0
flake==4.0.1
pytest==7.1.2
pytest-forked==1.4.0
pytest-xdist==2.5.0
selenium==4.1.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s create a folder called &lt;em&gt;“seltests”&lt;/em&gt; in the project’s root directory to house the test scripts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgx9ijnsnm96tq7d37a8j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgx9ijnsnm96tq7d37a8j.jpg" width="411" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before running the tests, please set the environment variables &lt;em&gt;LT_USERNAME &amp;amp; LT_ACCESS_KEY&lt;/em&gt; from the terminal. The account details are available on the &lt;a href="https://accounts.lambdatest.com/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Profile&lt;/a&gt; page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For macOS:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;export LT_USERNAME=LT_USERNAME&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;export LT_ACCESS_KEY=LT_ACCESS_KEY&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For Linux:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;export LT_USERNAME=LT_USERNAME&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;export LT_ACCESS_KEY=LT_ACCESS_KEY&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For Windows:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;set LT_USERNAME=LT_USERNAME&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;set LT_ACCESS_KEY=LT_ACCESS_KEY&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next sections of this blog on how to handle multiple windows in Selenium Python, we will look into the following use cases while handling windows in Selenium Python:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Handling a single child window in Selenium Python&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Handling multiple child windows in Selenium Python&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Switching back to a parent window from a child window in Selenium Python&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Need a great solution for &lt;a href="https://www.lambdatest.com/safari-browser-for-windows?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Safari for Windows&lt;/a&gt;? Forget about emulators or simulators — use real online browsers. Try LambdaTest for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling a single child window in Selenium Python
&lt;/h2&gt;

&lt;p&gt;Let us look at how to implement window handling on a single child window in the following test case scenario.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1- Navigate to the URL link &lt;a href="https://www.lambdatest.com/selenium-playground/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;2- Click on the “Window Popup Modal” link.&lt;/p&gt;

&lt;p&gt;3- In the new webpage, click on the “Follow on Twitter” button to open a child window.&lt;/p&gt;

&lt;p&gt;4- Switch to the child window opened.&lt;/p&gt;

&lt;p&gt;5- Print the window handle id of the child window.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Test Case on the Chrome Browser):&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
import pytest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions

def test_single_window_chrome():
    options = ChromeOptions()
    options.browser_version = '103.0'
    options.platform_name = 'Windows 10'
    lt_options = {}
    lt_options['username'] = os.environ.get ('LT_USERNAME')
    lt_options['accesskey'] = os.environ.get ('LT_ACCESS_KEY')
    lt_options['project'] = 'SingleWindowHandling'
    lt_options['selenium_version'] = '4.1.2'
    lt_options['w3c'] = True
    options.set_capability('LT:options', lt_options)
    # LambdaTest Profile username
    user_name = os.environ.get('LT_USERNAME')
    # LambdaTest Profile access_key
    accesskey = os.environ.get('LT_ACCESS_KEY')
    remote_url = "https://" + user_name + ":" + \
        accesskey + "@hub.lambdatest.com/wd/hub"
    driver = webdriver.Remote(remote_url, options=options)
    driver.get('https://www.lambdatest.com/selenium-playground')
    driver.maximize_window()
    # Look for the window popup modal link and click
    element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href="https://www.lambdatest.com/selenium-playground/window-popup-modal-demo"]')))
    element.click()
    # Get the window handle Id of the current parent window
    parent_guid = driver.current_window_handle
    print(f"The parent window guid is: {parent_guid}")
    twitter_element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[title="Follow @Lambdatesting on Twitter"]')))
    twitter_element.click()
    # Get the window handle ids of all the windows opened
    all_guid = driver.window_handles
    # iterate through the guids and if there is a parent window id skip it and switch to the new window
    for guid in all_guid:
        if guid != parent_guid:
            driver.switch_to.window(guid)
            print(f"The child guid is: {guid}")
            driver.close()

    driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To run the test scripts, type the following command in the terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pytest seltests/test_single_window_handling_chrome.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once the test is executed, the output in the terminal will display the GUIDS of the parent and child windows, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzqqf16g3xs973v12v940.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzqqf16g3xs973v12v940.png" width="800" height="229"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the test execution is completed, you can view the results and logs in the Build section of the &lt;a href="https://accounts.lambdatest.com/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Automation Dashboard&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsa1e9kko1ihnc70cdpy6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsa1e9kko1ihnc70cdpy6.png" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can view a test recording and logs of the tests executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2otyjtc886p5ekkaj3u0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2otyjtc886p5ekkaj3u0.png" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let us go through the code we have written in detail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Here, we import the important Selenium Python libraries that will enable us to perform our test. These include pytest, WebDriver, By, BrowserOptions, WebDriverWait, and expected_conditions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedrtsnfibzirip3szk9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fedrtsnfibzirip3szk9w.png" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We import pytest, as it is Selenium Python’s primary unit testing tool. The imported Selenium WebDriver is a web framework that automates cross browser tests.&lt;/p&gt;

&lt;p&gt;The By query is used to locate specific web elements using the find_element() method. The explicit wait time implements the WebDriverWait class combined with the expected_conditions class to define the wait for a certain condition to be executed before proceeding to the next part of the code.&lt;/p&gt;

&lt;p&gt;Lastly, the Options class customizes the specified browser driver sessions. For this instance, we are using the ChromeOptions classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: We generate options using the &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Automation Capabilities Generator&lt;/a&gt; according to the browser we use to run the automated tests. We create an instance of the Options class, set the conditions, and pass it to the driver constructor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzswumsns92q6btewhb9m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzswumsns92q6btewhb9m.png" width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: We set the username and accesskey provided in the LambdaTest Profile page to a remote_url variable that connects us to the Remote Selenium Grid (&lt;a class="mentioned-user" href="https://dev.to/hub"&gt;@hub&lt;/a&gt;.lambdatest.com/wd/hub). We then use the remote_url and options to instantiate the corresponding web browser (Chrome).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3q0vlrh0a0q2kq1e3vb3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3q0vlrh0a0q2kq1e3vb3.png" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; We open the LambdaTest Playground website, then maximize the browser window to locate the web element with the link to the Window Popup Demo Page.&lt;/p&gt;

&lt;p&gt;Right-click the “Windows Popup Modal” link and click inspect to open the browser inspect tool. The web element is located using its &lt;strong&gt;href&lt;/strong&gt; tag.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0w9x8yr7649onn1z4ogz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0w9x8yr7649onn1z4ogz.png" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use the &lt;strong&gt;driver. current_window_handle&lt;/strong&gt; method to obtain the parent window’s unique id and store it as a string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flyaf4z1nv7sd6egrwtqy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flyaf4z1nv7sd6egrwtqy.png" width="800" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; We then locate the web element with the button “Follow on Twitter” using its title and click on it to open a child window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1osrkjlmjac92hzmvlc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1osrkjlmjac92hzmvlc.png" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We store the window handles of the opened windows in a variable called &lt;em&gt;all_guid&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwu5hmuk476kwchh1wyl8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwu5hmuk476kwchh1wyl8.png" width="800" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; We then switch to the child window using the &lt;strong&gt;driver.switch_to.window(guid)&lt;/strong&gt; method. The code iterates through the stored &lt;em&gt;window handles&lt;/em&gt;, and if it comes across the parent window handle, it skips over it and switches to the child window. Then the child window’s ID is printed.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;driver.close()&lt;/em&gt; method closes the currently active child window and will quit the driver session if the child window is the only active window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc0gjrjsm2scfr3t59t1d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc0gjrjsm2scfr3t59t1d.png" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We then close the browser session using the &lt;em&gt;driverquit()&lt;/em&gt; method in Selenium.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you use a Mac and want to run the test in Internet Explorer? This article explores how to test &lt;a href="https://www.lambdatest.com/blog/test-internet-explorer-for-mac/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Internet explorer for Mac&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling multiple child windows in Selenium Python
&lt;/h2&gt;

&lt;p&gt;In the above section of this blog on how to handle multiple windows in Selenium Python, we have looked at how to handle a single child window in Selenium using Python. Let us go through how to implement window handling of multiple child windows in the scenarios below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Case (Switch Window by Page Title)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1- Navigate to the URL &lt;a href="https://www.lambdatest.com/selenium-playground/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;2- Click on the “Window Popup Modal’ link.&lt;/p&gt;

&lt;p&gt;3- In the new webpage, click on the “Follow Twitter &amp;amp; Facebook” button to open the child windows.&lt;/p&gt;

&lt;p&gt;4- Switch to each open child window using the page titles.&lt;/p&gt;

&lt;p&gt;5- Print the page title of each of the child windows.&lt;/p&gt;

&lt;p&gt;6- Close the browser session.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; import os
    import pytest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.options import Options as ChromeOptions
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    def test_multiple_windows_chrome():
        options = ChromeOptions()
        options.browser_version = '103.0'
        options.platform_name = 'Windows 10'
        lt_options = {}
        lt_options['username'] = os.environ.get ('LT_USERNAME')
        lt_options['accesskey'] = os.environ.get ('LT_ACCESS_KEY')
        lt_options['project'] = 'Multiple Window Handling Test'
        lt_options['selenium_version'] = '4.0.0'
        lt_options['w3c'] = True
        options.set_capability('LT:options', lt_options)
        # LambdaTest Profile username
        user_name = os.environ.get ('LT_USERNAME')
        # LambdaTest Profile access_key
        accesskey = os.environ.get ('LT_ACCESS_KEY')
        remote_url = "https://" + user_name + ":" + \
            accesskey + "@hub.lambdatest.com/wd/hub"
        driver = webdriver.Remote(remote_url, options=options)
        driver.get('https://www.lambdatest.com/selenium-playground')
        driver.maximize_window()
        # Look for the window popup modal link and click
        element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href="https://www.lambdatest.com/selenium-playground/window-popup-modal-demo"]')))
        element.click()
        # Get the window handle Id of the current parent window
        parent_guid = driver.current_window_handle
        parent_title = driver.title
        print(f"The parent window name is {parent_title} and its handle is: {parent_guid}")
        # Locate and click on the button "Follow Twitter &amp;amp; Facebook"
        web_element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.ID, 'followboth')))
        web_element.click()
        # Get the window handle ids of all the windows opened
        all_guid = driver.window_handles
        # Get the total number of windows of the open
        num_of_handles = len(all_guid)
        # print the numbers of window open
        print(f"The number of windows open {num_of_handles} ")

        # iterate through the total number of guids and if there is a parent window id skip it and switch to the new child windows
        for num in range(num_of_handles):
            if all_guid[num] != parent_guid:
                driver.switch_to.window(all_guid[num])
                print(f"The child window's title is: {driver.title}")
                driver.close()

        driver.quit()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We execute the test scripts in the terminal using the following command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pytest seltests/test_multiple_windows_handling_chrome.py 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The test runs successfully in the terminal and displays the following output, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fophhd5erhnqj5slvtwf6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fophhd5erhnqj5slvtwf6.png" width="800" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can view the test script’s results in the LambdaTest Builds section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcqf302aj14aimgwsrzmb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcqf302aj14aimgwsrzmb.png" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The test’s logs and recordings are also displayed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fct0uat169cx0c1pyssba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fct0uat169cx0c1pyssba.png" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The code written for implementing multiple child window handling in Selenium using Python is similar to that written for handling a single child window. Nonetheless, the difference comes when iterating through the window handles, as we show the distinction between the child windows based on their respective page titles.&lt;/p&gt;

&lt;p&gt;In the “Window Popup Modal” page, we locate the “Follow Twitter &amp;amp; Facebook” button using its &lt;strong&gt;ID&lt;/strong&gt; in the inspect tool. We click the button, which will then open up two child windows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmvmjvdn9h9x201amclad.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmvmjvdn9h9x201amclad.png" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9fxn2ah4tshxprdsk4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9fxn2ah4tshxprdsk4s.png" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The window handles of the currently open windows are stored in a variable called all_guid, and the total number of open windows is captured in the variable num_of_handles.&lt;/p&gt;

&lt;p&gt;The code iterates through all the window handles stored, identifies the child window handles, and switches to the child windows. The code checks the page title of the child window and prints it. The child windows are then closed using the driver.close() method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmaxyrz5hv5oxg1r0yhqq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmaxyrz5hv5oxg1r0yhqq.png" width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The driver session is then finally closed using the quit() method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Online &lt;a href="https://www.lambdatest.com/web-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;web testing&lt;/a&gt; on a cloud platform. Get instant access of Browsers like Safari, Chrome, Firefox, Opera, Yandex. 3000+ desktop &amp;amp; mobile environments. Be it automation testing or real time testing, we got you covered. Try for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Switching back to a parent window from a child window in Selenium Python
&lt;/h2&gt;

&lt;p&gt;Let us look at a scenario where we want to switch back to the parent window from the child window after we have completed some operations. This means we must change the WebDriver’s control to the main window.&lt;/p&gt;

&lt;p&gt;We will implement the scenario discussed in the above section of this blog on how to handle multiple windows in Selenium Python to demonstrate switching back to the parent window.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    import os
    import pytest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.options import Options as ChromeOptions
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    def test_switch_to_parent_window_chrome():
        options = ChromeOptions()
        options.browser_version = '103.0'
        options.platform_name = 'Windows 10'
        lt_options = {}
        lt_options['username'] = os.environ.get ('LT_USERNAME')
        lt_options['accesskey'] = os.environ.get ('LT_ACCESS_KEY')
        lt_options['project'] = 'Switch Back To Parent Window Test'
        lt_options['selenium_version'] = '4.0.0'
        lt_options['w3c'] = True
        options.set_capability('LT:options', lt_options)
        # LambdaTest Profile username
        user_name = os.environ.get ('LT_USERNAME')
        # LambdaTest Profile access_key
        accesskey = os.environ.get ('LT_ACCESS_KEY')
        remote_url = "https://" + user_name + ":" + \
            accesskey + "@hub.lambdatest.com/wd/hub"
        driver = webdriver.Remote(remote_url, options=options)
        driver.get('https://www.lambdatest.com/selenium-playground')
        driver.maximize_window()
        # Look for the window popup modal link and click
        element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href="https://www.lambdatest.com/selenium-playground/window-popup-modal-demo"]')))
        element.click()
        # Get the window handle Id of the current parent window
        parent_guid = driver.current_window_handle
        print(f"The Parent Window guid is: {parent_guid}")
        # Locate and click on the button "Follow Twitter &amp;amp; Facebook"
        web_element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.ID, 'followboth')))
        web_element.click()
        # Get the window handle ids of all the windows opened
        all_guid = driver.window_handles
        # Get the total number of windows of the open
        num_of_handles = len(all_guid)
        # print the numbers of window open
        print(f"The number of windows open {num_of_handles} ")

        # iterate through the total number of guids and if there is a parent window id skip it and switch to the new child windows
        for num in range(num_of_handles):
            if all_guid[num] != parent_guid:
                driver.switch_to.window(all_guid[num])
                print(f"The child window's title is: {driver.title}")
                driver.close()

        # Switch back to parent window
        driver.switch_to.window(parent_guid)
        print(f"The parent guid is: {parent_guid}")
        driver.quit()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Execution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We run the test script in the terminal using the following command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pytest seltests/test_switch_to_parent_window_chrome.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The test script will execute successfully in the terminal and display the following output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy0mbrml5iqqn3ykskudv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy0mbrml5iqqn3ykskudv.png" width="800" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can view the test results in the LambdaTest Builds dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F41zd1toa777oy8tatlue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F41zd1toa777oy8tatlue.png" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the test script, after navigating through the child windows and performing the operations we specified in the scenario before, the open child windows are closed. We then switch back to the parent window using the &lt;strong&gt;switch_to.window(parent_guid)&lt;/strong&gt; method with the parent_guid variable as the method’s argument. We then print the parent window’s handle.&lt;/p&gt;

&lt;p&gt;The driver session is finally closed using the driver.quit() method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpbtqr92lhdwhsiftwblf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpbtqr92lhdwhsiftwblf.png" width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are a Python programmer looking to take your career to the next level, you can go with the Selenium Python 101 certification from LambdaTest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Upload your app for testing within seconds using the LambdaTest cloud and perform mobile app testing right away. Find bugs early on, improve performance, quality, user experience and make the most of &lt;a href="https://www.lambdatest.com/mobile-app-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;mobile app test&lt;/a&gt; on LambdaTest.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this &lt;a href="https://www.lambdatest.com/learning-hub/python-tutorial?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=nov30_pk&amp;amp;utm_term=pk&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Python tutorial&lt;/a&gt;, we have covered in detail what windows are and how to handle multiple windows when they occur in testing using Selenium Python. We have looked at the two types of windows — the parent window and the child window. We have written test scripts that implement window handling for a single child window and multiple child windows. We have also looked at how to switch back to the parent window after completing operations on the child windows.&lt;/p&gt;

&lt;p&gt;I hope this blog on how to handle multiple windows in Selenium Python will help you while performing test automation.&lt;/p&gt;

</description>
      <category>gratitude</category>
      <category>watercooler</category>
    </item>
  </channel>
</rss>
