{"id":383,"date":"2024-06-11T13:19:59","date_gmt":"2024-06-11T13:19:59","guid":{"rendered":"https:\/\/istqblearning.com\/testing\/?page_id=383"},"modified":"2024-06-17T08:43:00","modified_gmt":"2024-06-17T08:43:00","slug":"create-selenium-python-script-login-form-successfully","status":"publish","type":"page","link":"https:\/\/istqblearning.com\/testing\/create-selenium-python-script-login-form-successfully\/","title":{"rendered":"Create Selenium Python script: login form successfully"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-3-1024x576.png\" alt=\"\" class=\"wp-image-446\" srcset=\"https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-3-1024x576.png 1024w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-3-300x169.png 300w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-3-768x432.png 768w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-3-1536x864.png 1536w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-3.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Copy and past the script below into login-form-successfully.py file. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-cyan-bluish-gray-background-color has-background\"><code>from selenium.webdriver.common.by import By<br>from selenium import webdriver<br>from selenium.webdriver.support import expected_conditions<br>from selenium.webdriver.support.wait import WebDriverWait<br><br># Creates a new instance of the chrome driver.<br>driver = webdriver.Chrome()<br># Loads https:\/\/istqblearning.com web page in the current browser session<br>driver.get(\"https:\/\/istqblearning.com\")<br><br># Wait up to 30 seconds for the website to appear<br>WebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, \"Home\")))<br><br># This account has been registered in https:\/\/istqblearning.com. You can register a new one.<br>username = \"tester\"<br>password = \"Test12345678@\"<br><br># Maximizes the current window that webdriver is using<br>driver.maximize_window()<br><br># Wait up to 30 seconds for the Log In menu to appear<br>WebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, \"Log In\")))<br><br># Find an element given a By strategy and locator<br>driver.find_element(By.LINK_TEXT, \"Log In\").click()<br><br># Wait up to 30 seconds for the login form to appear<br>WebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.ID, \"user_login\")))<br><br># Find an element given a By strategy and locator.<br>driver.find_element(By.ID, \"user_login\").send_keys(username)<br># Find an element given a By strategy and locator.<br>driver.find_element(By.ID, \"user_pass\").send_keys(password)<br><br>driver.find_element(By.ID, \"wp-submit\").click()<br><br># Wait up to 30 seconds for the Log Out menu to appear<br>WebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, \"Log Out\")))<br><br>driver.find_element(By.LINK_TEXT, \"Log Out\").click()<br># Wait up to 30 seconds for the Log In menu to appear<br>WebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, \"Log In\")))<br>driver.quit()<br><br>print(\"Log in and log out the https:\/\/istqblearning.com website successfully\")<\/code><br><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Step<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Opens a Chrome browser <\/td><\/tr><tr><td>2<\/td><td>Loads the <a href=\"https:\/\/istqblearning.com\/\">https:\/\/istqblearning.com\/<\/a> website from the current browser<\/td><\/tr><tr><td>3<\/td><td>Waits up to 30 seconds for the website to display successfully<\/td><\/tr><tr><td>4<\/td><td>Maximizes the website window<\/td><\/tr><tr><td>5<\/td><td>Waits up to 30 seconds for the Log In menu to appear<\/td><\/tr><tr><td>6<\/td><td>Clicks on Log In menu of the website<\/td><\/tr><tr><td>7<\/td><td>Waits up to 30 seconds for the Login form to appear<\/td><\/tr><tr><td>8<\/td><td>Inputs username into Username or Email Address textbox<\/td><\/tr><tr><td>9<\/td><td>Inputs password into Password textbox<\/td><\/tr><tr><td>10<\/td><td>Clicks on Log In button on the login form<\/td><\/tr><tr><td>11<\/td><td>Waits up to 30 seconds for the Log Out menu to appear. It means user has been logged successfully<\/td><\/tr><tr><td>12<\/td><td>Clicks on Log out menu of the website<\/td><\/tr><tr><td>13<\/td><td>Waits up to 30 seconds for the Log In menu to appear. It means user has been logged out successfully<\/td><\/tr><tr><td>14<\/td><td>Quits the current browser<\/td><\/tr><tr><td>15<\/td><td>Print the message in console &#8220;Log in and log out the <a href=\"https:\/\/istqblearning.com\">https:\/\/istqblearning.com<\/a> website successfully&#8221;<br><\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Scenario of the test script<\/figcaption><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">How to find out details of an element in a web page? We can use the Chrome browser&#8217;s Inspect feature<\/h5>\n\n\n\n<p>For example, at the step 3 of the scenario. We can check any element in the home page. In this case we will check Home element.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-4-1024x576.png\" alt=\"\" class=\"wp-image-454\" srcset=\"https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-4-1024x576.png 1024w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-4-300x169.png 300w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-4-768x432.png 768w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-4-1536x864.png 1536w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-4.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Right click on Home icon and the select Inspect feature.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-6-1024x576.png\" alt=\"\" class=\"wp-image-456\" srcset=\"https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-6-1024x576.png 1024w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-6-300x169.png 300w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-6-768x432.png 768w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-6-1536x864.png 1536w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Title-01-6.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Here is the statement of the step 3<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code># Wait up to 30 seconds for the website to appear<br>WebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, \"Home\")))<\/code><\/pre>\n\n\n\n<p>What is the link text locator? Selenium provides support for these 8 traditional location strategies in WebDriver. This <a href=\"https:\/\/www.selenium.dev\/documentation\/webdriver\/elements\/locators\/\">https:\/\/www.selenium.dev\/documentation\/webdriver\/elements\/locators\/<\/a> link explains all of them in details.<\/p>\n\n\n\n<p>Similar to the other steps in the scenario. We can use this method to their statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Copy and past the script below into login-form-successfully.py file. from selenium.webdriver.common.by import Byfrom selenium import webdriverfrom selenium.webdriver.support import expected_conditionsfrom selenium.webdriver.support.wait import WebDriverWait# Creates a new instance of the chrome driver.driver = webdriver.Chrome()# Loads https:\/\/istqblearning.com web page in the current browser sessiondriver.get(&#8220;https:\/\/istqblearning.com&#8221;)# Wait up to 30 seconds for the website to appearWebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, &#8220;Home&#8221;)))# This account [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-383","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/pages\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/comments?post=383"}],"version-history":[{"count":18,"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/pages\/383\/revisions"}],"predecessor-version":[{"id":662,"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/pages\/383\/revisions\/662"}],"wp:attachment":[{"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/media?parent=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}