{"id":70769,"date":"2020-01-07T14:49:40","date_gmt":"2020-01-07T09:19:40","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=70769"},"modified":"2024-04-12T14:22:20","modified_gmt":"2024-04-12T08:52:20","slug":"selenium-webdriver-api-by-example","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/","title":{"rendered":"Selenium-WebDriver API by Example"},"content":{"rendered":"<h1><strong>Selenium-WebDriver API by Example<\/strong><\/h1>\n<figure style=\"width: 545px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/hackr.io\/blog\/complete-guide-selenium-webdriver\/thumbnail\/large\" alt=\"Selenium-Webdriver API\" width=\"545\" height=\"341\" \/><figcaption class=\"wp-caption-text\">Selenium-Webdriver API<\/figcaption><\/figure>\n<p>In this section, we will understand the Selenium-WebDriver API by Example. Therefore, let\u2019s begin.<\/p>\n<p>WebDriver is a tool for automating web application testing and to verify that they work as expected. Moreover, it aims to provide a friendly API that\u2019s easy to explore and understand, which will help to make tests easier to read and maintain.<br \/>\nOnce the project is set up, you can see that WebDriver acts just like any normal library. In other words, you are entirely self-contained and you usually don\u2019t need to remember to start any additional processes before using it.<br \/>\nAdditional steps are required to use Chrome Driver, Opera Driver, Android Driver, and iOS Driver<br \/>\nYou\u2019re now ready to write some code. An example in Java language is:<\/p>\n<p>package org.openqa.selenium.example;<br \/>\nimport org.openqa.selenium.By;<br \/>\nSecondly, import org.openqa.selenium.WebDriver;<br \/>\nAfter that, import org.openqa.selenium.WebElement;<br \/>\nimport org.openqa.selenium.firefox.FirefoxDriver;<br \/>\nimport org.openqa.selenium.support.ui.ExpectedCondition;<br \/>\nLastly, import org.openqa.selenium.support.ui.WebDriverWait;<\/p>\n<p>public class Selenium2Example {<br \/>\npublic static void main(String[] args) {<br \/>\nFirstly, \/\/ Create a new instance of the Firefox driver<br \/>\nSecondly, \/\/ Notice that the remainder of the code relies on the interface,<br \/>\n\/\/ not the implementation.<br \/>\nWebDriver driver = new FirefoxDriver();<\/p>\n<p>\/\/ And now use this to visit Google<br \/>\ndriver.get(&#8220;http:\/\/www.google.com&#8221;);<br \/>\nFirstly, \/\/ Alternatively the same thing can be done like this<br \/>\nAfter that, \/\/ driver.navigate().to(&#8220;http:\/\/www.google.com&#8221;);<\/p>\n<p>\/\/ Find the text input element by its name<br \/>\nWebElement element = driver.findElement(By.name(&#8220;q&#8221;));<\/p>\n<p>\/\/ Enter something to search for<br \/>\nelement.sendKeys(&#8220;Cheese!&#8221;);<\/p>\n<p>\/\/ Now submit the form. WebDriver will find the form for us from the element<br \/>\nelement.submit();<\/p>\n<p>\/\/ Check the title of the page<br \/>\nSystem.out.println(&#8220;Page title is: &#8221; + driver.getTitle());<\/p>\n<p>\/\/ Google&#8217;s search is rendered dynamically with JavaScript.<br \/>\n\/\/ Wait for the page to load, timeout after 10 seconds<br \/>\n(new WebDriverWait(driver, 10)).until(new ExpectedCondition&lt;Boolean&gt;() {<br \/>\npublic Boolean apply(WebDriver d) {<br \/>\nreturn d.getTitle().toLowerCase().startsWith(&#8220;cheese!&#8221;);<br \/>\n}<br \/>\n});<\/p>\n<p>\/\/ Should see: &#8220;cheese! &#8211; Google Search&#8221;<br \/>\nSystem.out.println(&#8220;Page title is: &#8221; + driver.getTitle());<\/p>\n<h2><strong>Selenium-WebDriver API Commands and Operations<\/strong><\/h2>\n<p>In order to check what all we have in WebDriver, create a driver object from WebDriver and press the dot key. Further, this will list down all the methods of WebDriver. Methods followed by Object keyword are the generic methods get from Object Class in Java. Above all, you will find this method for every object of java language.<\/p>\n<p><strong>Method<\/strong><\/p>\n<p>A Java method is a collection of statements that comes together to perform an operation.<\/p>\n<ul>\n<li>Method Name: Firstly, to access any method of any class, we need to create an object of the class. After that, all the public methods will appear for the object.<\/li>\n<li>Parameter: Secondly, it is an argument that is passed to a method as a parameter to perform some operation. Moreover, every argument must pass with the same data type.<\/li>\n<li>Return Type: Lastly, the method can return a value or return nothing. That is to say, if the void is stated after the method, it means the method is returning no value. And if it is returning any value, then it must depict the type of the value.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-144226 aligncenter\" src=\"https:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2019\/12\/Image-7-2.png\" alt=\"\" width=\"419\" height=\"102\" \/><\/p>\n<p><strong>Fetching a Page<\/strong><\/p>\n<p>The first thing you would want to do with WebDriver is to navigate to a page. The normal way to do this is by calling \u201cget\u201d:<\/p>\n<p>driver.get(&#8220;http:\/\/www.google.com&#8221;);<\/p>\n<p>Dependent on several factors, including the OS\/Browser combination, WebDriver may or may not wait for the page to load. In some circumstances, WebDriver may return control before the page has finished, or even started, loading. To ensure robustness, you need to wait for the element(s) to exist in the page using Explicit and Implicit Waits.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Get certified and unlock more opportunities. Practice and Validate your skills to become a\u00a0<a href=\"https:\/\/www.vskills.in\/certification\/testing\/agile-testing-certification\" data-saferedirecturl=\"https:\/\/www.google.com\/url?q=https:\/\/www.vskills.in\/certification\/testing\/agile-testing-certification&amp;source=gmail&amp;ust=1579152864978000&amp;usg=AFQjCNEu1fdFuLWPniR55uUanyQjB80Z8w\">Certified Agile Testing Professional<\/a>\u00a0Now!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Selenium-WebDriver API by Example In this section, we will understand the Selenium-WebDriver API by Example. Therefore, let\u2019s begin. WebDriver is a tool for automating web application testing and to verify that they work as expected. Moreover, it aims to provide a friendly API that\u2019s easy to explore and understand, which will help to make tests&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8156],"tags":[8201],"class_list":["post-70769","page","type-page","status-publish","hentry","category-agile-testing","tag-selenium-webdriver-api-by-example"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Selenium-WebDriver API by Example - Vskills Tutorial<\/title>\n<meta name=\"description\" content=\"Learn and enhance your Selenium-WebDriver API by Example skills. Prepare and become a Certified Agile Testing Professional Now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Selenium-WebDriver API by Example - Vskills Tutorial\" \/>\n<meta property=\"og:description\" content=\"Learn and enhance your Selenium-WebDriver API by Example skills. Prepare and become a Certified Agile Testing Professional Now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:52:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/hackr.io\/blog\/complete-guide-selenium-webdriver\/thumbnail\/large\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/\",\"name\":\"Selenium-WebDriver API by Example - Vskills Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/hackr.io\/blog\/complete-guide-selenium-webdriver\/thumbnail\/large\",\"datePublished\":\"2020-01-07T09:19:40+00:00\",\"dateModified\":\"2024-04-12T08:52:20+00:00\",\"description\":\"Learn and enhance your Selenium-WebDriver API by Example skills. Prepare and become a Certified Agile Testing Professional Now!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/#primaryimage\",\"url\":\"https:\/\/hackr.io\/blog\/complete-guide-selenium-webdriver\/thumbnail\/large\",\"contentUrl\":\"https:\/\/hackr.io\/blog\/complete-guide-selenium-webdriver\/thumbnail\/large\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Selenium-WebDriver API by Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Selenium-WebDriver API by Example - Vskills Tutorial","description":"Learn and enhance your Selenium-WebDriver API by Example skills. Prepare and become a Certified Agile Testing Professional Now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/","og_locale":"en_US","og_type":"article","og_title":"Selenium-WebDriver API by Example - Vskills Tutorial","og_description":"Learn and enhance your Selenium-WebDriver API by Example skills. Prepare and become a Certified Agile Testing Professional Now!","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:52:20+00:00","og_image":[{"url":"https:\/\/hackr.io\/blog\/complete-guide-selenium-webdriver\/thumbnail\/large","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/","name":"Selenium-WebDriver API by Example - Vskills Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/#primaryimage"},"thumbnailUrl":"https:\/\/hackr.io\/blog\/complete-guide-selenium-webdriver\/thumbnail\/large","datePublished":"2020-01-07T09:19:40+00:00","dateModified":"2024-04-12T08:52:20+00:00","description":"Learn and enhance your Selenium-WebDriver API by Example skills. Prepare and become a Certified Agile Testing Professional Now!","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/#primaryimage","url":"https:\/\/hackr.io\/blog\/complete-guide-selenium-webdriver\/thumbnail\/large","contentUrl":"https:\/\/hackr.io\/blog\/complete-guide-selenium-webdriver\/thumbnail\/large"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/selenium-webdriver-api-by-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Selenium-WebDriver API by Example"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/70769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=70769"}],"version-history":[{"count":5,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/70769\/revisions"}],"predecessor-version":[{"id":76636,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/70769\/revisions\/76636"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=70769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=70769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=70769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}