Methods used in Selenium WebDriver for Automation

Selenium web driver in simple terms is an API library that contains a list of predefined classes and interfaces that contains predefined methods

Basic Methods that are used in Selenium web driver:

  • get() → Used to open specified URL’s web page, (HTTP:// needs to be provided)
  • manage()

manage().window().maximized(); → To maximize the window

manage().window().minimize(); → To minimize the window

  • findElement()

By class and its predefined methods: It’s a static method since Its a non-access modifier, so we can call this method with the help of the class name

  1. id()
  2. name()
  3. className()
  4. linkText()
  5. partialLinkText() → If the link text name is dynamically changing after each update then it is most useful to find that element
  6. cssSelector() → (tagname(attribute = ‘value’)
  7. XPath() → (//tagname(@attribute = ‘value’)

WebElement:

Predefined interface in selenium

Purpose → Store UI element located by the findElement() to reference variable of WebElement, useful for multiple operations in a single elements

click():

Used to perform click operations on different web elements like buttons, links, checkbox options, and radio option

sendKeys():

Used to enter text into text fields like text box, text area, password fields, etc.

clear():

Used to clear the text available in the text box or field area

getText():

Used to retrieve elements from the text (Text b/w the starting and ending tags of HTML elements), Store value in String

getTitle():

Used to retrieve the title of the current web page

getCurrentUrl():

Get to retrieve the current URL of the web page

close():

Used to close the current browser window (Active window)

quit():

Used to close all the browser windows (including child windows)

getAttribute():

Used to retrieve the value stored in a specific attribute value of an HTML element, need to pass the attribute

isDisplayed():

Used to find out whether the element is displayed on the page, before performing operations on it, returns a boolean value

isEnabled():

Used to find out whether the element is enabled or disabled before performing operations on it, returns a boolean value

isSelected():

Used to find out whether the radio options and checkbox options are selected or not, returns a boolean value

navigate():

Used to perform operations like navigating back to the previous page, navigating forward again, or refreshing the current

→ navigate().to();

→ navigate().back();

→ navigate().forward();

→ navigate().refresh();

getPageSource():

Used to retrieve all the source code of the current page and return in the form of a string

submit():

Used to submit a form, eg: search text field, always check for the form tag(locate any element on the form and then submit)

getTagName():

Used to get the HTML tag of the provided element, (It retrieves the tag name of the element)

getCSSValue():

To retrieve the CSS properties(styles → font-weight, height, width, etc.).

return type String

getSize():

Used to get the height and width of the given element, returns the Dimension value, from that we can get the height and width(px) of the button.

getLocation():

Used to get the x and y coordinate position of the given element, (x → from the left side of the page to the element, y → from the top of the page to the element),

returns Point value.

getRect():

Introduced in Selenium4

The return type is Rectangle

It gets the height, width, and location of the UI elements, and functions of both getSize(); and getLocation();

→ getX();

→ getY();

→ getWidth();

→ getHeight();

fullScreen():

Used to display a web page in full screen, other than maximized and minimized screen

driver().manage().window().fullScreen();

setSize():

Used to resize the browser window in specified dimensions.

Set the height and width of the browser window

The return type is Dimension.

driver().manage().window().setSize(w, h);

getClass():

Used to retrieve the class name of the provided object

(If chrome driver is the object reference then it will print ChromeDriver)

pageLoadTimeOut();

Used to change the default time for loading page

driver().manage().timeouts().pageLoadTimeOut(Duration.ofSeconds(3));

  • findElement() and findElements() methods.

findElement method is used to locate a single web element whereas the find elements method is used to locate multiple web elements from the web page.

Leave a comment

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