Selenium WebDriver Commands Cheat sheet

Following is the complete list of all commands available in Selenium WebDriver with their usage:

1. get(String s)

This command is used to open any webpage URL

driver.get (“https://www.qascript.com”) ;

2. navigate().to(Url url)
To navigate to a particular webpage URL, one can use the following command:

driver.navigate().to("https://qascript.com/contact-us");

3. navigate().forward()

The forward command navigates the browser forward by one page recorded in the browsing history.

driver.navigate().forward();

4. navigate().back()

The back command instructs the browser to redirect to the immediate previous webpage.

driver.navigate().back();

5. navigate().refresh()

The Refresh command instructs the browser to reload or refresh the current web page.

driver.navigate().refresh();

6. getTitle()

This method fetches the Title of the current page. Accepts nothing as a parameter and returns a String value.

String title = driver.getTitle();

7. getCurrentUrl()

This method fetches the string representing the Current URL which is opened in the browser. Accepts nothing as a parameter and returns a String

String url = driver.getCurrentUrl();

8. getPageSource()

This method returns the Source Code of the page. Accepts nothing as a parameter and returns a String value.

String source = driver.getPageSource();

9. close()

This method closes only the current window the WebDriver is currently controlling.

driver.close();

10. quit()

This method Closes all windows opened by the WebDriver.

driver.quit();

11. clear()

This method clears out the values within an element.

WebElement txtUsername = driver.findElement(By.id("Username"));
txtUsername.clear();

12. sendKeys(charSequence…charSequences)

This simulates typing into an element, which may set its value. This method accepts charSequence as a parameter and returns nothing.

WebElement txtUsername = driver.findElement(By.id("username"));
txtUsername.sendKeys("QASCRIPT");

To press any key from the keyboard using sendkeys:

WebElement btnLogin = driver.findElement(By.id("submit"));
btnLogin.sendKeys(Keys.Enter);

13. click()

This command simulates the click event on any element.

WebElement btnLogin = driver.findElement(By.id("submit"));
btnLogin.click();

14. isDisplayed()

This method determines if an element is currently being displayed or not in the web page.

boolean status = driver.findElement(By.id("login")).isDisplayed();

15. isEnabled()

This method determines if the element currently is Enabled or not in the web page.

boolean status = driver.findElement(By.id("login")).isEnabled();

16. isSelected()

This method determines whether or not element is selected or not. This operation only applies to input elements such as Checkboxes, Select Options, and Radio Buttons.

boolean status = driver.findElement(By.id("male")).isSelected();

17. submit()

This method works well/better than the click() if the current element is a form, or an element within a form.

driver.findElement(By.id("submitbutton")).submit();

18. getText()

This method will fetch the text of the given element and returns a string value.

String text = driver.findElement(By.id("logo")).getText();

19. getTagName()

This method gets the tag name of this element.

String tag = driver.findElement(By.id("submit").getTagName();

20. getCssValue()

This method Fetch CSS property value of the give element.

driver.findElement(By.id("element")).getCssValue();

21. getAttribute()

This method gets the value of the given attribute of the element.

String val = driver.findElement(By.id("button")).getAttribute("name");

22. getSize()

This method fetch the width and height of the rendered element.

Dimensions dimension = driver.findElement(By.id("button")).getSize();
System.out.println("Height: " + dimenstion.height + "Width: " + dimension.width);

23. getLocation()

This method locate the location of the element on the page.

WebElement element = driver.findElement(By.id("SubmitButton"));
Point point = element.getLocation();
System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y);

24. selectByVisibleText(String s)

This method selects the option that displays the text matching the parameter.

Select ddCountries = new Select(driver.findElement(By.id("country")));
ddCountries.selectByVisibleText("UK");

25. deselectByVisibleText(String text)

This method deselects the option that displays the text matching the parameter.

Select ddCountries = new Select(driver.findElement(By.id("country"))); 
ddCountries.deselectByVisibleText("UK");

26. selectByIndex(int index)

This method selects the option that matches with the given index.

Select ddCountries = new Select(driver.findElement(By.id("country"))); 
ddCountries.selectByIndex(1);

27. deselectByIndex(int index)

This method deselects the option that matches with the given index.

Select ddCountries = new Select(driver.findElement(By.id("country"))); 
ddCountries.deselectByIndex(1);

28. selectByValue(String value)

This method selects the option that matches with the given value.

Select ddCountries = new Select(driver.findElement(By.id("city"))); 
ddCountries.selectByValue("London");

29. deselectByValue(String value)

This method deselects the option that matches with the given value.

Select ddCountries = new Select(driver.findElement(By.id("city"))); 
ddCountries.deselectByValue("London");

30. getAllSelectedOptions()

This method returns the list of all selected options associated with a Select tag.

Select ddCountries = new Select(driver.findElement(By.id("city")));
List<WebElement> allSelectedOptions = ddCountries.getAllSelectedOptions()

31. getFirstSelectedOption()

This method returns the first selected option associated with a Select tag.

Select ddCountries = new Select(driver.findElement(By.id("city"))); 
WebElement selectedOption = ddCountries.getFirstSelectedOption();

32. deselectAll()

The method deselects all the selected options associated with Select tag.

Select ddCountries = new Select(driver.findElement(By.id("city"))); 
WebElement selectedOption = ddCountries.deselectAll();

33. getOptions()

The method returns the list of all the option tags associated with a Select tag.

Select ddCountries = new Select(driver.findElement(By.id("city"))); 
List<WebElement> allOptions = ddCountries.getOptions();

34. isMultiple()

This method tells whether the SELECT element support multiple selecting options at the same time or not.

Select ddCountries = new Select(driver.findElement(By.id("city")));
ddCountries.isMultiple();

35. manage().timeouts().implicitlyWait(long l, TimeUnit timeunit)

The implicit wait will tell to the web driver to wait for certain amount of time before it throws a “No Such Element Exception”. The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception.

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

36. manage().timeouts().pageLoadTimeout(long l, TimeUnit timeunit)

This method sets the amount of time to wait for a page load to complete before throwing an error.

driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);

37. manage().timeouts().SetScriptTimeout(long l, TimeUnit timeunit)

This method sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.

driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);

38. manage().window().maximize()

This method will maximize the current window of the browser.

driver.manage().window().maximize();

39. manage().window().fullscreen()

This method will fullscreen the current window if it is not already fullscreen

driver.window().fullscreen();

40. manage().window().getPosition()

This method will Get the position of the current window, relative to the upper left corner of the screen.

Point point = driver.manage().window().getPosition();
System.out.println(“X offset : “+point.x);
System.out.println(“Y offset : “+ point.y);

41. manage().window().setPosition()

Used to set the position of the current window, relative to the upper left corner of the screen.

Point newPoint = new Point(300, 500);
driver.manage().window().setPosition(newPoint);

42. manage().window().setSize()

Point newPoint = new Point(300, 500);
driver.manage().window().setSize(newDimension);

43. manage().deleteAllCookies()

This method deletes all cookies.

driver.manage().deleteAllCookies();

44. manage().deleteCookie(Cookie cookie)

This method deletes the specific cookie.

driver.manage().deleteCookie(arg0);

45. manage().deleteCookieNamed(String s)

This method deletes the specific cookie according to the name.

driver.manage().deleteCookieNamed(arg0);

46. manage().getCookies()

This method returns list of all cookies.

driver.manage().getCookies();

47. manage().getCookieNamed(String s)

This method returns specific cookie according to name

driver.manage().getCookieNamed(arg0);

48. findElement()

A command used to uniquely identify a web element within the web page.

WebElement element = driver.findElement(By.id("login"));

49. findElements()

A command used to identify a list of web elements within the web page.

List<WebElement> elements = driver.findElements(By.tag("a"));

50. getWindowHandle()

Method to get the window handle of the current window.

String handle = driver.getWindowHandle();

51. getWindowHandles()

Method to get the window handle of all the current windows.

Set<String> handles = driver.getWindowHandles();

52. switchTo().activeElement()

Switches to the element that currently has focus within the document currently “switched to”, or the body element if this cannot be detected.

driver.switchTo().activeElement();

53. switchTo().alert()

Method to switch to a popup alert.

Alert alert = driver.switchTo().alert();

54. switchTo().defaultContent()

Selects either the first frame on the page, or the main document when a page contains iframes.

driver.switchTo.defaultContent();

55. switchTo().frame()

Method to switch to another frame using a frame name.

driver.switchTo().frame("frameName");

56. switchTo().parentFrame()

Method to change focus to the parent context.

driver.switchTo().parentFrame();

57. switchTo().window()

Method to switch to another window using a window handle or name.

driver.switchTo().window("windowName");

If you know any other selenium command which is missing from the above list then please leave a comment below.

Share

Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

2 Responses

  1. Aneelkumar says:

    Great,Very helpful, Thanks Bijan

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!