{"id":2553,"date":"2023-10-30T17:53:28","date_gmt":"2023-10-30T17:53:28","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=2553"},"modified":"2023-12-03T06:47:50","modified_gmt":"2023-12-03T06:47:50","slug":"selenium-form-webelement-textbox-submit-button-sendkeys-click","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2023\/10\/30\/selenium-form-webelement-textbox-submit-button-sendkeys-click\/","title":{"rendered":"Handle WebElements: TextBox, SubmitButton using sendKeys and Click methods"},"content":{"rendered":"\n<p><strong>What you will Learn in this blog:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u2018sendkeys\u2019 method in selenium to type text in a text field<\/li>\n\n\n\n<li>Usage of click() method in selenium<\/li>\n\n\n\n<li>Usage of clear() method in selenium<\/li>\n\n\n\n<li>Usage of submit() method in selenium<\/li>\n\n\n\n<li>Source code<\/li>\n<\/ol>\n\n\n\n<p><strong><br><\/strong><strong>\u2018sendkeys\u2019 method in selenium to type text in a text field<\/strong><\/p>\n\n\n\n<p>Let us now see the usage of \u2018sendkeys\u2019 method in selenium to type some text in a text field.&nbsp;<\/p>\n\n\n\n<p>Launch <a href=\"https:\/\/www.selenium.dev\/downloads\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.selenium.dev\/downloads\/<\/a>&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Dnj7_hJeGGPFDZPNl0T2Ym8lD6GNT_6sMqk8WtoeRqfSmZAQDc2fmkyQpJZnUGOa9zIOx-RjhmdFYgY9QuS4KproYpGrleEqJb2n09kJW6LQOMwm8LsGNZsSVbkk6XjF-l3qCLuHQks14iwpT5ND\" alt=\"\"\/><\/figure>\n\n\n\n<p>Click \u2018API Docs\u2019 link under \u2018Java\u2019. The below page comes up<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/VM-YuK4iK0gxnPIpz2ePhs1enhERF7QDO0fM-HyChiiXoCVmnce2NaWS0NeF__iLg07pSzrLMaxb5E8nV1uG-1oKDGQT8ulia-2MzUCkMdd1ylyFzhjTp_9HHRF4HLvAyKVbRdkTkXZ7W_sGttyH\" alt=\"\"\/><\/figure>\n\n\n\n<p>Search for \u2018By\u2019. You will see search results<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/KFh9D11jX9GXDS-oBSM0Jrf-HNkF_socyK5GFvS1hVMn8ZriPjpoNcvF362USx6isjkPce39cZ_Y3AFBHY4OseEE71m_2P-7CfRdPLZu0yp7ZQ0_zwa_AkNdSlKQ5H7-iJ5Cr_WahJnMyK-9_EDI\" alt=\"\"\/><\/figure>\n\n\n\n<p>Click the first search result shown above.<\/p>\n\n\n\n<p>The selenium API has class \u2018<em>By<\/em>\u2019 that helps in identifying the elements on a webpage<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/TjV4DTWc_ZHIGQtZPzdsQAtVHVilCn73HR29YOwn1S84HKmpIlxYRzmDxQEMlR9o_m1eKQW0K31_o-wN3ol8qc0rsWPMQO5E9XZsJJzN-VVyAP1ThQhBVU5xOyGexcKMDk4WjsYFCsJypeAMBzXu\" width=\"602\" height=\"449\"><\/p>\n\n\n\n<p>Open chrome browser window and launch <a href=\"https:\/\/rahulshettyacademy.com\/angularpractice\/\">https:\/\/rahulshettyacademy.com\/angularpractice\/<\/a>&nbsp;<\/p>\n\n\n\n<p>Inspect the \u2018Name\u2019 field.&nbsp;<\/p>\n\n\n\n<p>Notice below that this field is represented by attribute \u2018name\u2019 having the value \u201cname\u201d <strong><br><\/strong><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/XqRr7I7DA3Cy2FXoGtZIMpNQhqYIwns1CNGMQ8fHPPvjUrzH2EogVIqGT7EffoHAp1u2KZq4U3BrBNB5xIMDBcdbILhqUrhhYysiFwBc_kPlK6ANklmCJRn9F0PcqzJMC4LMOTSF9OIIDubV3Thj\" width=\"575\" height=\"471\"><\/p>\n\n\n\n<p>See line#17 that shows the usage of <strong><em>By.name<\/em><\/strong> method<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/AMdwoRAzkPRrXszpq5ODjNRizPoAyo8a--Pn-I-pJ9g90VaurLaRdSYHeSm17Ir0qbC-pS9e5vdlRSxDbDjJqTsLtMJJME8hijA_b_tEyfVYpIjP5WpOCnNY2VlI5RfninXTydivoI3Jjh_6H5BJ\" alt=\"\"\/><\/figure>\n\n\n\n<p>So we can copy the value of \u2018name\u2019 attribute &amp; use it as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/v4B1-fBEIzDJ3wQs3l0OYLGuT7VOx_pg46nx-xzhQ28-WGv3Zweq9Cl0X-bkjb8dNMFpvCLSR8yRCATphTnx2C0DqSD0QymjyL1RxdF5KgTyXJTSXQHYbPjaaGlZIwwU2P3RM1O2ONnOkGT6yTnu\" alt=\"\"\/><\/figure>\n\n\n\n<p>Next, we can further chain the \u2018<strong><em>sendkeys<\/em><\/strong><strong>\u2019<\/strong> method that will help us to simulate the typing of keys<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/x156cEg3GGB6N9bTqdIXfvvsvzIjRnW2JzmXvKNsLurr5T_GVdSqB42yw6XMrbBe05s1YeOcevlItHhfoT5QtFnPaWUXvQx8xIS93JFuxf0IK21NEgqRMfvfL75vSeXl443XbVKCWjAyGLB7Z2Fw\" alt=\"\"\/><\/figure>\n\n\n\n<p>So if we want to simulate the typing of \u201ctestuser1\u201d, the \u2018sendkeys\u2019 method can do it for us<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Zywtqh2YaKUGxFIXto1SX94-dLOZLyF1THJKmpqcuFc1KotPS3H2-l7Zm7_TstknyXF1GY--bU35DAKWWPAv6onFKMCUGjRMOzs3uKttbHzXIUuSRdQxS8xwQv7na-LW9N8YE7mL0IhPcuCqBnij\" alt=\"\"\/><\/figure>\n\n\n\n<p>Save and execute the script.&nbsp;<\/p>\n\n\n\n<p>Notice below that the desired text gets typed in the text field<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/TM6A54j-SMLpIhOsJPWIaYY9xRhhm6zkvnqiK8ryiK43L8XwmVhXfAzAbzS9LwR2B_XiEUH6x4_dqWldDrgaNbXIaE4ASc0VLoRi2DTAvocfzMighbAtKJAx5oBX0PkvhCp7b4RcK9ML34QpsilQ\" width=\"485\" height=\"463\"><\/p>\n\n\n\n<p><strong>Usage of click() method in selenium<\/strong><\/p>\n\n\n\n<p>Go to <a href=\"https:\/\/courses.rahulshettyacademy.com\/courses\">https:\/\/courses.rahulshettyacademy.com\/courses<\/a>&nbsp;<\/p>\n\n\n\n<p>You will see lot of courses<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/FFNfifKXI3IGybN13lSNmnMLfeeUd1q6hI-QrGhf9Rrl4R6KLPaNcHk3ntX5-L_v4H1k7rTrw1abdOPFcF0bPiNit3HSnSziJzWECTNGelY2p13VmAE5NAKv_5rVsRhGR4iZKYcn-JzdQeLDZUtY\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us inspect the first course \u201cALL-ACCESS Subscription\u201d<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/WAewOJ34OoxejyB74cttgjv5HMJu6cbLIimkt2i5ciT_3Asj3cqV4sGW_aPYWGAd1jSDyv6U7MrhxOYNObJVczKN9pq83KxgOhxOQOt7lj2IsdxcTWLsNx_PTRmxiNh5PDv_cQyZCJBIdnwVaR_A\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can create xpath to identify this element (this element is present under a \u2018div\u2019 tag whose attribute is \u2018title\u2019 and the value of attribute is \u2018All-Access Membership\u2019)<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Umxe8_ddtFkPJoEB0yTJVIAkD_CM9MBFE44tzIyRYsHm5jFGKzv7DXt_73PClJxgDdwFCfu9GwNgo9M_7uyq5HlfQuBUciU2YPonmvYhUDENycXxBw0tEkT_9RCiK9t5qZcEQeVB-sMtgU6OgVF6\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can now use the \u2018click()\u2019 method to click this element<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/grJHNHMZApx4g2Lez5V2MsGEbtOCHg4iydIZOLjfbpRAvMboT7NR23kqbyE9BFoweV-yl4xiIZ6O6XtCFgRFaxIRpa5lcT600tZrWIUxfu2YslgTJvPXUVKHPhz8MWj2lZtnHDb42NRlT3y3iysH\" alt=\"\"\/><\/figure>\n\n\n\n<p>Save and execute.&nbsp;<\/p>\n\n\n\n<p>Notice below that the course link is clicked and the course\u2019s detail page comes up<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/SHqYLGwWWLNKwNoMdY_ZultJFjuRNUVkGIh3-KH88DytUMCvHulr7EKn5dpgM5IIrFPm8kLodWMD_qYcgD0S6msgKdBtKhVN0mSJSgtdnXYAxePk58IA422XYhkzhQs_s6nRxxGKBAGHLD0X351K\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Usage of clear() method in selenium<\/strong><\/p>\n\n\n\n<p><br>The \u2018<strong><em>clear()<\/em><\/strong>\u2019 method simply clears the text from the textbox. See below. We are entering text \u201ctestuser1\u201d in line#28.&nbsp;<\/p>\n\n\n\n<p>In line#29 we are using the \u2018clear()\u2019 method to delete or clear the same text&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/0W9VpV5usIdOVUqerN0PmXYcWZR516AC9CjuC-1txQwWCdXosAiRNOSPtxWmaoyEQqPt86ugkCVeIPVx3kA2wc4q0k0psx9L33293s3lfdeLcfgQ6RQapMCwtDWq8NcQeyo3ddjQ2joEUJEqGsUe\" alt=\"\"\/><\/figure>\n\n\n\n<p>Save and run.&nbsp;<\/p>\n\n\n\n<p>Notice below that the \u2018Name\u2019 field becomes empty\/clear<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/VWsCHxHyK2FjlP6VlYUYVTmpZwxaHuau1DmIQrO9ZD45Wx41ewb41xKdFby_oc77yiHKbLUB19ofeC7TVYPz4WALrXOogsKunsCFfNudBW5mK3GCb3cohxCyEP9_bZNkaZhE8gIDh8tWi_uFsOgz\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Usage of submit() method in selenium<\/strong><\/p>\n\n\n\n<p><br>Launch <a href=\"https:\/\/rahulshettyacademy.com\/angularpractice\/\">https:\/\/rahulshettyacademy.com\/angularpractice\/<\/a>&nbsp;<\/p>\n\n\n\n<p>Enter value in \u2018Name\u2019 field<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/AXS9whax2hT58Qq-RSIpZfx28xjobU2cil_NDDaHui_c9Cr8HtLUg5u7xfutUHe69rRDw_suKxr7bYsUoowpBNXyAIXm6o35HDcK94Gf2hj_W4yjya4aZQBrl2jHZQkXBuPocP19X8FCbxirMQSO\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can see the \u2018Submit\u2019 button above to submit the form.<\/p>\n\n\n\n<p>Let us click \u2018Submit\u2019. Notice that we get the \u2018Success\u2019 message<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ieUYftaKOLElRV3iCd08VE0CVYybm9pfLsFHDw5-nbNLIkCZ4uTxZHNKzKMU5S-cuV6-DvDU9Qxy5dDTozWlgVUQERKu2fg6JWfg2we_yyuG4xmO0kqULsqZzh3CVCZnb46kWh85iqpu-QnYP2iB\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let us automate these manual steps<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/2_4MiRF4lYEVqfgzvThSdNsB3cdoUv93XRq_LcRmgscFfJw2FAmKx6Wa8sZ-hD-Gpd_yOouN11XuUJGG6A6RmgA3Qsu4c94quZq2gIAloD7QRaic8P0E6C-McmQxz_H89dcd6Ec_Kd1CGXbZkFkW\" alt=\"\"\/><\/figure>\n\n\n\n<p>Notice below that the form has been submitted successfully<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/C7J3r98K6_lTP-t3A9xuHv6RS_PTCv1o1fK59Flm8cev43LQVMuPoYHvR4A7j8GOaUKpJoUARgMvhopek-Q3tDFN4uCHOFxEM1iDGpt4F9LpEqk4DzLbPn5YkyRGXtDnU86zeNzjkSmi4n71Kjal\" alt=\"\"\/><\/figure>\n\n\n\n<p>So this is how we can handle some of the form web elements using selenium.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">Source code<\/span><\/strong><\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<pre class=\"wp-block-code\"><code>package Intro;\nimport org.openqa.selenium.By;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.firefox.FirefoxDriver;\n\n\nimport io.github.bonigarcia.wdm.WebDriverManager;\npublic class SeleniumScript {\n\tpublic static void main(String&#91;] args) {\n\t\tWebDriverManager.firefoxdriver().setup();\n\t\tWebDriver driver = new FirefoxDriver();\n\/\/sendkeys method\n\t\t\/*\t\tdriver.get(\"https:\/\/rahulshettyacademy.com\/angularpractice\/\");\t\tdriver.findElement(By.name(\"name\")).sendKeys(\"testuser1\");\n\t\t*\/\n\/\/click method\n\t\t\/*\t\tdriver.get(\"https:\/\/courses.rahulshettyacademy.com\/courses\");\n\t\tdriver.findElement(By.xpath(\"\/\/div&#91;@title='All-Access Membership']\")).click();\n\t\t*\/\n\/\/clear method\n\t\t\/*\n\t\tdriver.get(\"https:\/\/rahulshettyacademy.com\/angularpractice\/\");\t\tdriver.findElement(By.name(\"name\")).sendKeys(\"testuser1\");\n\tdriver.findElement(By.name(\"name\")).clear();\n\t\t*\/\n\/\/submit method\t\tdriver.get(\"https:\/\/rahulshettyacademy.com\/angularpractice\/\");\t\tdriver.findElement(By.name(\"name\")).sendKeys(\"testuser1\");\n\tdriver.findElement(By.name(\"name\")).submit();\n}\n}\n<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/ZSFz-Qr_rzfiGcJy9t76lSSs4iAGUHr9jwiEMcOhLDsl33_3ZJTKk_1wGd6f9hE1IuIeTIcaCMQy8tanvvL9BPGDvE76muceH9WsmS7Q66XR3gL7bAkXK_Uqg9qt2T-d_kS-Hh3F29HEY5bv_ZmZ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Thank you for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What you will Learn in this blog: \u2018sendkeys\u2019 method in selenium to type text in a text field Let us now see the usage of \u2018sendkeys\u2019 method in selenium to type some text in a text field.&nbsp; Launch https:\/\/www.selenium.dev\/downloads\/&nbsp; Click \u2018API Docs\u2019 link under \u2018Java\u2019. The below page comes up Search for \u2018By\u2019. You will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1238,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2553"}],"version-history":[{"count":11,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2553\/revisions"}],"predecessor-version":[{"id":2815,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2553\/revisions\/2815"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1238"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}