Selenium Interview Questions and Answers with Examples – 2

Selenium Interview Questions and Answers with Examples – 2 is our second post in this series.  Readers can get the first part

In this post, we are going to discuss about Selenium Interview Questions and Answers with Examples in detail. I am going to write this series which will be well suitable for Freshers as well as experienced candidates. Please post me whatever Selenium Interview Questions with or without Answers and with / without examples. Let us try to consolidate everything under one roof here.

Selenium Interview Questions and Answers with Examples – 2

Today, We shall go through – how to get the list of URLs displayed in Google Result Page.  This is one of the important interview question being asked in Selenium Interviews.  I shall share the Java Program for the same here with.  In this program, after printing all the links (identified with ahref) and we click on the third link available in the list of links.

package payilagam.webdriver.learning;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class GoogleSearchLearning {

public static void main(String[] args) {
// Setting up Chrome Driver
System.setProperty(“webdriver.chrome.driver”, “E://Selenium//chromedriver_win32//chromedriver.exe”);

WebDriver webDriver = new ChromeDriver();

webDriver.get(“https://www.google.com”);

          // Identifying Search Inbox
WebElement searchBoxElement = webDriver.findElement(By.xpath(“//input[@name=’q’]”));
// Passing values to Search Box
searchBoxElement.sendKeys(“vikram”); // send also a “\n”
//  element.sendKeys(Keys.ENTER);
// Pressing Enter button
searchBoxElement.submit();

// wait until the google page shows the result – Explicit Wait
//WebElement myDynamicElement =
(new WebDriverWait(webDriver, 10))
.until(ExpectedConditions.
presenceOfElementLocated(By.id(“resultStats”)));

List<WebElement> mySearchList = webDriver.findElements(By.xpath(“//*[@id=’rso’]//h3/a”));

// All the links will be printed here
for (WebElement webElement : mySearchList)
{
System.out.println(webElement.getAttribute(“href”));
}
//   mySearchList.get(2).getAttribute(“href”);
//Get the url of third link and navigate to it
//  mySearchList.get(2).getAttribute(“href”);
String third_link = mySearchList.get(2).getAttribute(“href”);
webDriver.get(third_link);
webDriver.quit();
}
}