{"id":64274,"date":"2024-10-15T16:30:00","date_gmt":"2024-10-15T11:00:00","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=64274"},"modified":"2025-10-31T11:55:48","modified_gmt":"2025-10-31T06:25:48","slug":"generative-ai-with-python-selenium","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/generative-ai-with-python-selenium\/","title":{"rendered":"Generative AI with Python Selenium"},"content":{"rendered":"\n<p>In the world of automation, Selenium has long been a cornerstone for web applications. However, the advent of Generative AI has introduced a new dimension to automation which promises to enhance efficiency, accuracy, and creativity. <\/p>\n\n\n\n<p>This blog post delves into the exciting intersection of Generative AI with Python Selenium exploring how Python can be leveraged to harness the power of both technologies.<\/p>\n\n\n\n<p>By combining the robust web automation capabilities of the Selenium framework with the innovative potential of Generative AI, automation testers can create more intelligent, adaptive, and efficient automation solutions. Let us see more about it in this article!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Prerequisites for Generative AI with Python Selenium<\/strong><\/h2>\n\n\n\n<p>For the <a href=\"https:\/\/www.guvi.in\/blog\/what-is-generative-ai\/\" target=\"_blank\" rel=\"noreferrer noopener\">Generative AI<\/a> in action with Python Selenium we need to have a problem statement first. With that problem we will request the Google Gemini AI model to write an automation testing test-case.<\/p>\n\n\n\n<p>Followed by which we will see and evaluate whether the code works and what more we can do to enhance the quality of our Python code.<\/p>\n\n\n\n<p>The journey is going to be exciting as we humans will be going to evaluate the <a href=\"https:\/\/www.guvi.in\/hub\/python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a> code written by a Generative AI model.<\/p>\n\n\n\n<p>The problem statement for our Blog is:-&nbsp;<\/p>\n\n\n\n<p><em>\u201cWrite a Python script for automation testing using Python Selenium framework and Pytest to login into the OrangeHRM dashboard using the Username &#8220;admin&#8221; and Password &#8220;admin123&#8221;. \u201c<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeC5Y-e7dw6xqNb4spLGmO2tYqS52YpPaet44sD8E7V4GQAqFplKkCdvnjtXtdmf0iJCB6ho_fflsrqR5Rid97aBQCHhtIseCEzi4UeGYli0LAQYruEBGtyCy5dVWzXayPpTtlpXZE-fKHqZb7lYRWpsTxw?key=MLt_aftE5WdcO3nAWeSMCg\" alt=\"Python script for automation testing\" title=\"\"><\/figure>\n\n\n\n<p>Now let us see what type of Python automation testing code the <a href=\"https:\/\/gemini.google.com\/?hl=en-IN\" target=\"_blank\" rel=\"noreferrer noopener\">Google Gemini AI<\/a> model makes for us!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfnlEKZRKBfKPQ4bmhwNAEXT9ljHeOdlyt7m5jqfIMebhwfQlQ0mgAm9GqY_FSq5ySwuVVFSB9LRAlWLglj441uDaIeL50hjiPfqJg7j06wgZXgiX7TifoAguQjryDhhXsI5rQKIXaCaJliNSk9Q4gv-QY?key=MLt_aftE5WdcO3nAWeSMCg\" alt=\"Python automation testing code Google Gemini AI \" title=\"\"><\/figure>\n\n\n\n<p>The full code is as follows<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pytest\nfrom selenium import webdriver\nfrom selenium.webdriver.common.by import By\n\n\n@pytest.fixture\ndef driver():\n    driver = webdriver.Chrome()  # Replace 'chromedriver' with the path to your ChromeDriver\n    yield driver\n    driver.quit()\n\n\ndef test_orangehrm_login(driver):\n    # Navigate to OrangeHRM login page\n    driver.get(\"https:\/\/opensource-demo.orangehrmlive.com\/web\/index.php\/auth\/login\")\n\n\n    # Find username and password fields\n    username_field = driver.find_element(By.ID, \"username\")\n    password_field = driver.find_element(By.ID, \"password\")\n\n\n    # Enter credentials\n    username_field.send_keys(\"admin\")\n    password_field.send_keys(\"admin123\")\n\n\n    # Submit login form\n    login_button = driver.find_element(By.XPATH, \"\/\/button&#91;@type='submit']\")\n    login_button.click()\n\n\n    # Assert successful login (e.g., check for a specific element on the dashboard)\n    assert \"Dashboard\" in driver.title\n<\/code><\/pre>\n\n\n\n<p>Now, if we look into the code it is quite certain that it will work and run properly. Great! We do not need to write any code as Generative AI will take all the pains of automation testers now.<\/p>\n\n\n\n<p>But ! Here is a Catch !!! The code fails to follow the rules and guidelines set by Page Object Model (POM).&nbsp;&nbsp;<\/p>\n\n\n\n<p>The correct folder structure is&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXddxcRTzS1-7QftE1Dls_5TPwJkeqYcWz3wh7t6wwJFx5RW5D38Ex_fGURxGNSmMcrFH-3hSs9wHgddlY1JvxZ70rZ7M7YCvp9u4G2R9oNH66L-LH3JKcDf-OwCdF6A3jfGmMlyBb6AaaO1bQeU1-hg_XpU?key=MLt_aftE5WdcO3nAWeSMCg\" alt=\"folder structure\" title=\"\"><\/figure>\n\n\n\n<p>The entire code is given below which follows the Page Object Model (POM)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># data.py\n\n\n# This file consists of Test Information like username, password, XPATH etc\n\n\n# Python Class for Username and Password\nclass Suman_Data:\n    username = \"Admin\"\n    password = \"admin123\"\n\n\n# Python Class for Selenium Selectors\nclass Suman_Selectors:\n    input_box_username = \"username\"\n    input_box_password = \"password\"\n    login_xpath = '\/\/*&#91;@id=\"app\"]\/div&#91;1]\/div\/div&#91;1]\/div\/div&#91;2]\/div&#91;2]\/form\/div&#91;3]\/button'\n\n# testSuman.py\nfrom selenium import webdriver\nfrom selenium.webdriver.common.by import By\nfrom Test_Data import data\nimport pytest\nimport time\n\n\nclass Test_Suman:\n    url = \"https:\/\/opensource-demo.orangehrmlive.com\"\n   \n    # Booting Method for running the Python Tests\n    @pytest.fixture\n    def booting_function(self):\n        self.driver = webdriver.Firefox()\n        yield\n        self.driver.close()\n   \n    def test_get_title(self, booting_function):\n        self.driver.get(self.url)\n        assert self.driver.title == 'suman'\n        print(\"SUCCESS # Web Title Captured Successfully\")\n   \n    def test_login(self, booting_function):\n        self.driver.get(self.url)\n        time.sleep(5)\n        self.driver.find_element(by=By.NAME, value=data.Suman_Selectors.input_box_username).send_keys(data.Suman_Data.username)\n        self.driver.find_element(by=By.NAME, value=data.Suman_Selectors.input_box_password).send_keys(data.Suman_Data.password)\n        self.driver.find_element(by=By.XPATH, value=data.Suman_Selectors.login_xpath).click()\n        assert self.driver.title == 'OrangeHRM'\n        print(\"SUCCESS # LOGGED IN WITH USERNAME {username} and PASSWORD {password}\".format(username=data.Suman_Data.username, password=data.Suman_Data.password))\n<\/code><\/pre>\n\n\n\n<p>From the codes written by Generative Model and by human-being we can say one thing that <a href=\"https:\/\/www.guvi.in\/courses\/machine-learning-and-ai\/generative-ai\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=generative-ai-with-python-selenium\" target=\"_blank\" rel=\"noreferrer noopener\">Generative AI<\/a> is going to help us to write day-to-day codes without any issues but to manage and maintain code quality we need human interference which can ensure that the code which is being written will be&nbsp;<\/p>\n\n\n\n<ul>\n<li>Reusable&nbsp;<\/li>\n\n\n\n<li>More readable<\/li>\n\n\n\n<li>Sans with duplication<\/li>\n<\/ul>\n\n\n\n<p>In case you want to learn more about automation testing with Selenium, consider enrolling for HCL GUVI\u2019s certified\u00a0<a href=\"https:\/\/www.guvi.in\/zen-class\/selenium-automation-testing-course\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/zen-class\/selenium-automation-testing-course\/\" target=\"_blank\" rel=\"noreferrer noopener\">Automation Testing with Selenium Course<\/a>\u00a0online course that teaches you everything from scratch and also provides you with an industry-grade certificate!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Therefore, we can say that the combination of Python Selenium and Generative AI opens up a world of possibilities for automation, and testing. By leveraging the strengths of both technologies we can automate repetitive tasks, generate personalized content, and enhance user experiences. As Generative AI and automation continue to evolve we can expect to see even more groundbreaking advancements in the future and a bright future for budding and working automation testers as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the world of automation, Selenium has long been a cornerstone for web applications. However, the advent of Generative AI has introduced a new dimension to automation which promises to enhance efficiency, accuracy, and creativity. This blog post delves into the exciting intersection of Generative AI with Python Selenium exploring how Python can be leveraged [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":64282,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[933,40],"tags":[],"views":"8117","authorinfo":{"name":"Suman Gangopadhyay","url":"https:\/\/www.guvi.in\/blog\/author\/suman-gangopadhyay\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/10\/Generative-AI-with-Python-Selenium-300x116.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/10\/Generative-AI-with-Python-Selenium.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/64274"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=64274"}],"version-history":[{"count":11,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/64274\/revisions"}],"predecessor-version":[{"id":92133,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/64274\/revisions\/92133"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/64282"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=64274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=64274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=64274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}