Selenium driver methods

To work with selenium, we need to know the Selenium WebDriver methods..

Mainly these terms need to know:

  1. get() methods
  2. Locating links by linkText() and partialLinkText()
  3. Selecting multiple items in a drop dropdown
  4. Submitting a form
  5. Handling iframes
  6. alert() methods
  7. close() and quit() methods

We will discuss every actions:

get() methods

get()The command launches a new browser and opens
the specified URL in the browser instance
getClass()The command is used to retrieve the Class object
that represents the runtime class of this object
getCurrentUrl()The command is used to retrieve the URL of the webpage the user is currently accessing
getPageSource()The command is used to retrieve the page source
of the webpage, the user is currently accessing
getTitle()The command is used to retrieve the title of the webpage the user is currently accessing
getText()The command is used to retrieve the inner text
of the specified web element
getAttribute()The command is used to retrieve the value of the specified attribute
getWindowHandle()the command helps us switch to the newly opened window and performs actions on the new window.
getWindowHandles()This command helps to deal with multiple windows

linkText() is used to access a link with proper text.

partialLinkText() is used to access a link with partial text.

Selecting multiple items in a drop dropdown

There are primarily two kinds of dropdowns:

  1. Single select dropdown: A drop-down that allows only a single value to be selected at a time.
  2. Multi-select dropdown: A drop-down that allows multiple values to be selected at a time.

Code to automate multiple items from multi-select dropdown:

HTML Design:
<select id="selectId" multiple="">
<option value="redvalue">Red</option>
<option value="bluevalue">Blue</option>
<option value="yellowvalue">Yellow</option>
<option value="greenvalue">Green</option>
</select>

Selenium to select multiple values from the drop-down:

// select the multiple values from a dropdown
Select selectByValue = new Select(driver.findElement(By.id("SelectID_One")));
selectByValue.selectByValue("greenvalue");
selectByValue.selectByVisibleText("Red");
selectByValue.selectByIndex(2);

Submitting a form

it locates the submit button element and triggers the submit() method that submits user form.

Handling iframes

Following command helps user to switch iframe from the parent window and access iframe element.

driver.switchTo().frame(“ID of the iframe“);

Following command helps user to switch from iframe to parent window and access iframe element.

driver.switchTo().defaultContent(); 

alert() methods

If any alert shows on webpage after submitting a form or a button click, then alert() method is used.

There are 4 types of usage of alert manipulation:

  1. Accept the alert box
  2. Dismiss the alert box
  3. Get text from the alert box
  4. Write text in alert box
//To click on the 'OK' button of the alert.
driver.switchTo().alert().accept();
//To click on the 'Cancel' button of the alert
driver.switchTo().alert().dismiss(); 
//To capture the alert message
driver.switchTo().alert().getText();
// To send some data to alert box.
driver.switchTo().alert().sendKeys("Text");

close() and quit() methods

There are two types of commands in WebDriver to close the web browser instance.

a) close(): WebDriver’s close() method closes the web browser window that the user is currently working on which window is currently accessed by the WebDriver.

b) quit(): Unlike close() method, quit() method closes down all the windows that the program has opened.

about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

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