{"id":16538,"date":"2024-06-10T09:18:47","date_gmt":"2024-06-10T03:48:47","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=16538"},"modified":"2024-06-10T10:02:43","modified_gmt":"2024-06-10T04:32:43","slug":"selenium-user-interactions","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/selenium-user-interactions\/","title":{"rendered":"Selenium User Interactions"},"content":{"rendered":"\n<p>Selenium Webdrivers will be the most frequently used tool that are available in the selenium tool set. It is also important to understand how to use the selenium to interact with the webapps. In the module, we have to interact the actions that are against those GUI objects like:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.<strong>Text Box Interactions<\/strong><\/h2>\n\n\n\n<p>We may put values to text box using the \u2018sendkeys\u2019 method. We can also retrieve text from the text box using the getattribute(\u201cValue\u201d)command.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/docsz\/AD_4nXcWBedNTD5yWxQxXduIgpw4GX3L3wmMlZvLiKY5EjBkQ_AOocbG9MTexBiO6mHgyD_6CVXLEP3iZ4SOaZGeJUPEz5YUzA4amK82SI75h2RjcgrFYrz3q2WA2ZB2HZGACBdxwnDO-dbAIEsaB1Wrgu6xPFS4Zh5Xf_eFCt7ZypVkDuQQpdpFpA?key=PhEAYgdUTz1oKZDnfY-bnQ\" alt=\"selenium_ide_177\" title=\"\"><\/figure>\n\n\n\n<p>For example:<\/p>\n\n\n\n<p>import java.util.concurrent.TimeUnit;<\/p>\n\n\n\n<p>import org.openqa.selenium.*;<\/p>\n\n\n\n<p>import org.openqa.selenium.firefox.FirefoxDriver;<\/p>\n\n\n\n<p>public class webdriverdemo {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;public static void main(String[] args) throws InterruptedException {&nbsp; \/\/This is the main function<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebDriver driver = new FirefoxDriver();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ It will put a Implicit wait, Will wait for 10 seconds before throwing exception<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/To&nbsp; Launch website<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.navigate().to(&#8220;http:\/\/www.calculator.net\/percent-calculator.html&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ To Maximize the browser<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.manage().window().maximize();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp; when we enter value 10 in the first number of the percent Calculator<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.findElement(By.id(&#8220;cpar1&#8221;)).sendKeys(&#8220;10&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(5000);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ text box from the application<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String result = driver.findElement(By.id(&#8220;cpar1&#8221;)).getAttribute(&#8220;value&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/It Prints a Log In message to the screen<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system.out.println(&#8221; The Result &nbsp; &#8221; + result);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Close the Browser.<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.close();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>Output<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/docsz\/AD_4nXdD4-uEhDJvgbdRKp20u7bKUNFTxL4m-vmDiPQZMI7dTK15enD0eGUJQBPVGJ7SoaGvrILVNSRS3wSoG4NohO4TjK5EM9fde8GoPqD0jbVH4zc3X8OQ6E2W2mv182ii459Wd6FMzxhUmb0pk5rlO271i71xLHkMsRxngb5tjE2NYm-3WGVDDA?key=PhEAYgdUTz1oKZDnfY-bnQ\" alt=\"selenium_ide_183\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">2.<strong>Radio Button Selection<\/strong><\/h2>\n\n\n\n<p>We can select Radio button by using \u2018Click\u2019 method and also unselect using the same \u2018click \u2018method. Consider an example, how we interact with the radio button using <a href=\"https:\/\/www.calculator.net\/mortgage-payoff-calculator.html\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.calculator.net\/mortgage-payoff-calculator.html<\/a>. It can also check if a radio button is selected.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/docsz\/AD_4nXfx4EMPe_4YtoU1P2hoUibubif6KBFuijiu9qKQILLHoZtSu-6-z1qsiH2RWiI_2hpxYfihCuCIV6f9SWpN9hLnBbze1HPXAmhNIdPB_rZATbbTHXKt4kWBu1d0XbmXTSV9R0xoaslOjOKdVSDbrK1_TSfBle_WY0leBzn3_ERTVrNce1BoyBE?key=PhEAYgdUTz1oKZDnfY-bnQ\" alt=\"selenium_ide_178\" title=\"\"><\/figure>\n\n\n\n<p>import java.util.concurrent.TimeUnit;<\/p>\n\n\n\n<p>import org.openqa.selenium.*;<\/p>\n\n\n\n<p>import org.openqa.selenium.firefox.FirefoxDriver;<\/p>\n\n\n\n<p>public class webdriverdemo {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;public static void main(String[] args) throws InterruptedException {&nbsp; \/\/It will show an exception<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebDriver driver = new FirefoxDriver();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/It will put an Implicit wait, Will wait for 10 seconds before throwing exception<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ To Launch website<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.navigate().to(&#8220;http:\/\/www.calculator.net\/mortgage-payoff-calculator.html&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.manage().window().maximize();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ To Click on Radio Button<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.findElement(By.id(&#8220;ccpayoff1&#8221;)).click();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system.out.println(&#8220;The Output IsSelected &#8221; +<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.findElement(By.id(&#8220;ccpayoff1&#8221;)).isSelected());<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system.out.println(&#8220;The Output IsEnabled &#8221; +<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.findElement(By.id(&#8220;ccpayoff1&#8221;)).isEnabled());<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system.out.println(&#8220;The Output IsDisplayed &#8221; +<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.findElement(By.id(&#8220;ccpayoff1&#8221;)).isDisplayed());<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/Close the Browser.<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.close();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>Output<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/docsz\/AD_4nXdE4yygGcsvdf1fHDhrONu56XbqLT_IAdh2rf6OIvz7rc8d-ltM69UHFOSKAd2ndSyBAFG9zF0N4ItZ_esTxRuGJE-QKWLoHNisL7V9h9kLhuu3SM6cFTNKltKH7Vqmu0OqaOvoeo7HWO2W3QfduZYs5SEMlLtNIuc7tONgnp7hH31bavD2LQ?key=PhEAYgdUTz1oKZDnfY-bnQ\" alt=\"selenium_ide_179\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3.<strong>Synchronization<\/strong><\/h2>\n\n\n\n<p>Here to synchronise between the script execution and application we need to wait after performing appropriate actions.<\/p>\n\n\n\n<p><strong>Thread.Sleep<\/strong><\/p>\n\n\n\n<p>Thread.Sleep is a static wait and it is not good way to use in the script without conditions.<\/p>\n\n\n\n<p>Thread.Sleep(1000);<\/p>\n\n\n\n<p><strong>Explicit Waits<\/strong><\/p>\n\n\n\n<p>Explicit waits for a certain condition to occur before proceeding further. It will be mainly used when we want to click or may act an object once it is visible.<\/p>\n\n\n\n<p>WebDriver driver = new FirefoxDriver ();<\/p>\n\n\n\n<p>driver.get (&#8220;Enter an URL&#8221;S);<\/p>\n\n\n\n<p>WebElement DynamicElement =&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;(new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated (By.id (&#8220;DynamicElement&#8221;)));<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Implicit wait<\/strong><\/h2>\n\n\n\n<p>Implicit Wait is mainly used in cases where the Web driver will not locate the object immediately because of its non availability. The Webdriver will wait for specified implicit wait time and also not try to find the element again during the specified time period.<\/p>\n\n\n\n<p>When the specified time limit is crossed the webdriver will try to search the element will be once again for one last time.It proceeds with execution upon failure it throws exception.<\/p>\n\n\n\n<p>WebDriver driver = new FirefoxDriver();<\/p>\n\n\n\n<p>driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);<\/p>\n\n\n\n<p>driver.get(&#8220;Enter an URL&#8221;);<\/p>\n\n\n\n<p>WebElement DynamicElement = driver.findElement(By.id(\u201cDynamicElement&#8221;));<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FluentWait<\/strong><\/h2>\n\n\n\n<p>A FluentWait instance may explain the maximum amount of time to wait for condition to take place as well as the frequency with which check the existence of an object condition.<\/p>\n\n\n\n<p>Wait wait =&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;new FluentWait(driver).withTimeout(60, SECONDS).pollingEvery(10, SECONDS).ignoring(NoSuchElementException.class);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;WebElement dynamicelement = wait.until(new Function&lt;webdriver,webElement&gt;() {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;public WebElement apply(WebDriver driver) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return driver.findElement(By.id(&#8220;dynamicelement&#8221;));<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>});<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Checkbox Interaction<\/strong><\/li>\n<\/ol>\n\n\n\n<p>We can select check box by using the \u2018click\u2019 method and uncheck by using the same \u2018click\u2019 method. Consider to interact with a check box using can <a href=\"https:\/\/www.calculator.net\/mortgage-calculator.html.%2520We\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.calculator.net\/mortgage-calculator.html. We<\/a> can also see if checkbox is visible.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/docsz\/AD_4nXc83GSOWIsjw3j5ce9O_CrQQ5NC7jpUk1xZhSV8hqQqrR0G3c1UOFyM0x0EsIketAqlVyjkzFXhwwMzF131YI3U_L9__kR_Ug1HfcVfa8-9IcAxcRBqcc_xAcSJT-vZdFdS9jP2Da-pH8vWkrlANgSdLKjnZzOCncNd0SgWPQ8VnuAhko0MZrk?key=PhEAYgdUTz1oKZDnfY-bnQ\" alt=\"selenium_ide_180\" title=\"\"><\/figure>\n\n\n\n<p><br>public class webdriverdemo {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;public static void main(String[] args) throws InterruptedException {\/\/throws an exception<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebDriver driver = new FirefoxDriver();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;It will put a Implicit wait, Will wait for 10 seconds before throwing exception<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/Launch website<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.navigate().to(&#8220;http:\/\/www.calculator.net\/mortgage-calculator.html&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.manage().window().maximize();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/Click on check Box<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.findElement(By.id(&#8220;caddoptional&#8221;)).click();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system.out.println(&#8220;The Output&nbsp; IsSelected &#8221; +<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.findElement(By.id(&#8220;caddoptional&#8221;)).isSelected());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system.out.println(&#8220;The Output&nbsp; IsEnabled &#8221; +<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.findElement(By.id(&#8220;caddoptional&#8221;)).isEnabled());<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system.out.println(&#8220;The Output&nbsp; IsDisplayed &#8221; +<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.findElement(By.id(&#8220;caddoptional&#8221;)).isDisplayed());<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.close();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}&nbsp;<\/p>\n\n\n\n<p>Output<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/docsz\/AD_4nXcI31q9tkSMJk8xpedYZ1ejhaq8svmrI4eSRemlTW5XXTptEi22XIWb59d3zzPI-BMCNM8-N3b8rRurRh_3rOxtIU3RQMtUNJSYemCuk3ErANs-wyrs2JtWr9ZvdpzAvEVz-SpiixlJ1xNGEuP2FeUJOb7SXNcsFd8mRegzOpzEi9FBLZo0lw?key=PhEAYgdUTz1oKZDnfY-bnQ\" alt=\"selenium_ide_181\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Questions:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u00a0What is Selenium User Interactions?<\/li>\n\n\n\n<li>\u00a0What is Explicit and Implicit interactions explain with example?<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Selenium Webdrivers will be the most frequently used tool that are available in the selenium tool set. It is also important to understand how to use the selenium to interact with the webapps. In the module, we have to interact the actions that are against those GUI objects like: 1.Text Box Interactions We may put [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":16551,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-16538","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/16538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=16538"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/16538\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/16551"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=16538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=16538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=16538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}