To work with selenium, we need to know the Selenium WebDriver methods..
Mainly these terms need to know:
- get() methods
- Locating links by linkText() and partialLinkText()
- Selecting multiple items in a drop dropdown
- Submitting a form
- Handling iframes
- alert() methods
- 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 |
Locating links by linkText() and partialLinkText()
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:
- Single select dropdown: A drop-down that allows only a single value to be selected at a time.
- 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:
- Accept the alert box
- Dismiss the alert box
- Get text from the alert box
- 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.
Leave a Reply