HomeAutomation

Selenium Java Interview Questions And Answers – Part 22

Selenium Java Interview Questions And Answers – Part 22

Selenium Java Interview Questions and Answers Part-22

1) How to select an option in list box?

We can use the select class and methods to select an option in list box.

Select select = new Select(element);
select.selectByVisibleText("QA");

 

2) What is an iframe and how to handle it using Selenium WebDriver?

An iframe is an HTML element which allows an external webpage to be embedded in another HTML document. An iframe can be handled in Selenium using the switchTo() methods.

driver.switchTo().frame(element);

 

3) How do you count the total number of rows in a web table?

List<WebElement> rows = driver.findElements(By.xpath("//table[@id=webtable]/tbody/tr"));
int count = rows.size();

 

4) Write an xpath to find all the hyperlinks on a web page?

List<WebElement> links = driver.findElements(By.tagName("a"));

 

5) What are the different XPath types and which XPath type you have used in your projects?

There are 2 types of XPath:
– Absolute XPath: It starts with a single slash(/). It traverses from the root node till the element in the HTML DOM
– Relative XPath: It starts with a double slash(//). It can search element anywhere on the HTML DOM

6) An element has an id “bng_123” but its number is changing. How to handle it?

We can handle the dynamic elements using different xpath functions.

driver.findElement(By.xpath("//input[starts-with(@id,'bng_')]));

 

7) How to get text from hidden elements?

We can get the innerHTML value of the hidden element using the getAttribute() method.

String text = driver.findElement(element).getAttribute("innerHTML");

 

8) What is the return type of driver.getWindowHandles() in Selenium?

Return type for getWindowHandles is a Set<String>

9) In a web page, how will you ensure that the page has been loaded completely?

We can wait for a particular webelement on the page to become clickable by using explicit wait.

10) What is the difference between build and perform methods in Actions Class?

– build() method in Actions class is use to create chain of action or operation you want to perform.
– perform() method in Actions Class is use to execute chain of action which are build using Action build method.

11) What is the use of sleep() ?

Selenium Webdriver waits for the specified amount of time, whether the element is present or not.

12) What is the difference between Selenium Grid and Selenium WebDriver?

Selenium WebDriver allows us to directly interact with different browsers by using different client libraries. Selenium Grid allows us to execute webdriver scripts on remote machines.

13) How to select a checkbox present in a grid?

We can select a checkbox using the click operation.

14) While using click() command, can you use screen coordinates?

Actions action = new Actions(driver);
action.moveByOffset(X,Y).click().perform();

 

15) Explain how you can switch back from a frame?

We can switch back to main frame by using the following methods:
– driver.switchTo().parentFrame();
– driver.switchTo().defaultContent();

16) How to close browser popup window in Selenium WebDriver?

We can use the following methods of Alert interface to close popup windows:
– To click on the cancel button of the alert

driver.switchTo().alert().dismiss();

– To click on the OK button of the alert

driver.switchTo().alert().accept();

 

17) Give the example for method overload in Selenium?

In Selenium, Implicit wait is an example of Method Overloading. In Implicit wait we use different time stamps such as SECONDS, MINUTES, HOURS, etc…

18) How to invoke an application in Selenium WebDriver?

Runtime runtime = Runtime.getRuntime();
runtime.exec("Notepad.exe");

 

19) How to change the URL on a web page using Selenium WebDriver?

We can use the navigate.to() method to change the URL of a page.

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

 

20) What is the use of contextClick()?

ContextClick() is used to perform the right click operation on an element.

21) Count the number of links in a page?

List<WebElement> links = driver.findElements(By.tagName("a"));
int count = links.size();

 

22) How to select all the check boxes in a page?

List<WebElement> cb = driver.findElements(By.xpath("//input[@type='checkbox']"));
for(WebElement element: cb){
element.click();
}

 

23) What is Actions class in WebDriver and its methods?

Actions class enables us to handle different mouse and keyboard events. Some of the methods are:
doubleClick(): Performs double click on the element
clickAndHold(): Performs click on the mouse without releasing it
dragAndDrop(): Drags the element from one point and drops to another
moveToElement(): Moves the mouse pointer to the center of the element
contextClick(): Performs right-click on the mouse
sendKeys(): Sends a series of keys to the element
keyUp(): Performs key release
keyDown(): Performs key press without release

24) How to switch to another window using Selenium?

We can use the switchTo().window(windowHandle) to switch to another window.

25) What does Thread.sleep() method does?

Thread.sleep() is a static Java method that suspends the code execution for a specific amount of time.

Next Steps:

> More interview questions and answers on Selenium Java, continue to the next post (Click on Next Post link below)

> Check complete Selenium Java interview questions and answers here (Click here)

Please leave your questions/comments/feedback below.

Happy Learning ?

About Me > Bijan Patel

Connect to me on Linked In (Click here)

On a mission to contribute to the Software Testing Community in all possible ways.

Comments (0)

Leave a Reply

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

Open chat
Contact Us on Whatsapp