Hello guys, In this blog I am going to provide most frequently asked selenium-related interview questions and answers in part-wise.
Part 1
1. How to handle dynamic elements in Selenium?
Solution – We can use x-path along with contains(), starts-with(), ends-with() methods –
depends on the scenario.
Example –
Let’s say, ‘Id’ of a username field is – username_123
If you open the page again, then Id is showing as - username_245
That’s mean, the first part – username is static, whereas, second part after underscore is dynamic.
x-path for such scenario will be = //*[contains(@id ,’username’)]
Since the starting text is static, the above scenario can also be handled using starts-with() method.
x-path = //*[starts-with(@id ,’username_’)]
Let’s say, ‘Id’ of a username field is – 123_username
If you open the page again, then Id is showing as – 245_username
Here the latter part is static.
X-path = //*[ends-with(@id ,’_username’)]
2. Give an example of Method overloading in Selenium.
Solution - Example can be switching to Frames.
· frame(int index)
· frame(string frame_name)
· frame(WebElement element)
1 3. How do you achieve synchronization in Selenium?
S
S Solution – driver.manage().timeOuts().implicitlyWait(10, OutputType.SECONDS);
It
il It will ask the web driver to wait for 10 seconds before throwing an exception in case any web driver is unable to locate any element. It is applicable to every command written in the script.
1
4. How to invoke an application in web driver?
S Solution –
· driver.get(“application_url”);
· driver.navigate().To(“application_url”);
1 5. How to get the width and height of the textbox?
S
Solution –
· driver.findElement(By.xpath(“locator”)).getSize().getWidth();
· driver.findElement(By.xpath(“locator”)).getSize().getHeigtht();
6. Difference between Assert, Verify and WaitFor commands.
S
o Solution –
· Assert – It checks whether an element is on the page or not. If the element is not present, then the test will fail, and execution will stop at that point only.
· Verify - It checks whether an element is on the page or not. If the element is not present, then the test will fail, but execution will continue until the last command of the script.
· WaitFor – It waits for the specified amount of time so that the element may be available on the page. But if the element is not available within the specified amount of time, then the test will fail at that point only.
1 7. Different ways to refresh the browser.
S Solution –
· Driver.navigate().refresh();
· Driver.navigate().to(driver.getCurrentUrl());
· Driver.get(driver.getCurrentUrl());
1 8. Different exceptions encountered in Selenium WebDriver.
S
S Solution - Below are some most encountered Exceptions that occur while creating Selenium Automation Script.
- NoSuchFrameException
- AlertNotPresentException.
- NoSuchElementException.
- ElementNotVisibleException.
- NOSuchWindowException.
- TimeOutException.
To get a detailed explanation, follow the link - Exceptions In Selenium
9. What is the alternate way to click on the Login button?
S Solution –
submit() – But can be used only when attribute ‘type=submit’.
1
10. What is the purpose of getOptions() methods?
S
o Solution – It is used to get all the selected options from the dropdown list.
Return Type - List<WebElement>
Below program will print all the options of the Country drop-down -
Return Type - List<WebElement>
Below program will print all the options of the Country drop-down -
package com.javascript;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class GetOption {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://newtours.demoaut.com/mercuryregister.php");
driver.manage().window().maximize();
Select select = new Select(driver.findElement(By.xpath("//select[@name='country']")));
List<WebElement> options = select.getOptions();
for(WebElement option : options)
{
System.out.println(option.getText());
}
}
}