• Slider Banner

    Prepare for

    Technical Interview

    Over .1 Million+ Interview Questions for different profiles

    Read More
  • Slider Banner

    Join the

    Programming World

    Learn about top programming languages, tools and frameworks.

    Read More
  • Slider Banner

    Prepare with

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read More

Explain how can you find broken links in a page using Selenium WebDriver?

The interviewer can provide a situation where in there are few links in a web page, and you have to verify which of those links are working and how many are not working (broken).

Since you need to verify the working of every link, the workaround is that, you need to send http requests to all of the links on the web page and analyze the response. Whenever you use driver.get() method to navigate to a URL, it will respond with a status of 200 – OK. 200 – OK denotes that the link is working and it has been obtained. If any other status is obtained, then it is an indication that the link is broken.

First, we have to use the anchor tags <a> to determine the different hyperlinks on the web page. For each <a> tag, we can use the attribute ‘href’ value to obtain the hyperlinks and then analyze the response received for each hyperlink when used in driver.get() method.

Explain how you will login into any site if it is showing any authentication popup for username and password using Selenium WebDriver?

If its Window popup then we have to use Robot Class or AutoIT. For Web Based popup, we need to use the explicit command and verify if the alert is actually present. Only if the alert is present, we need to pass the username and password credentials. The sample code for using the explicit wait command and verifying the alert is below:

WebDriverWait wait = new WebDriverWait(driver, 10); 
Alert alert = wait.until(ExpectedConditions.alertIsPresent()); 
alert.authenticateUsing(new UserAndPassword(**username**, **password**))

Can we enter text without using sendKeys() in Selenium WebDriver?

Yes. We can enter/send text without using sendKeys() method. We can do it using JavaScriptExecutor.

Using DOM method of, identification of an element, we can go to that particular document and then get the element by its ID (here username) and then send the text by value. Look at the sample code below:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById("username").value=myusername");

How to switch to a new window (new tab) which opens up after you click on a link using Selenium WebDriver?

If you click on a link in a web page, then for changing the WebDriver’s focus/ reference to the new window we need to use the switchTo() command. Look at the below example to switch to a new window:

String handle = driver.getWindowHandle();

//get the name of all the windows that were initiated by the WebDriver
for (String handle : driver.getWindowHandles()) 
{
    driver.switchTo().window(handle);
}

How to set the size of browser window using Selenium?

To maximize the size of browser window, you can use the following piece of code: 

driver.manage().window().maximize(); 

To resize the current window to a particular dimension, you can use the setSize() method. Check out the below piece of code:

System.out.println(driver.manage().window().getSize());
Dimension d = new Dimension(500, 600);
driver.manage().window().setSize(d);

To set the window to a particular size, use window.resizeTo() method. Check the below piece of code:

((JavascriptExecutor)driver).executeScript("window.resizeTo(1024, 768);");

How to send ALT/SHIFT/CONTROL key in Selenium WebDriver?

When we generally use ALT/SHIFT/CONTROL keys, we hold onto those keys and click other buttons to achieve the special functionality. So it is not enough just to specify keys. ALT or keys. SHIFT or keys. CONTROL functions.

For the purpose of holding onto these keys while subsequent keys are pressed, we need to define two more methods:  and 


keyDown(modifier_key)

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Purpose: Performs a modifier key press and does not release the modifier key. Subsequent interactions may assume it's kept pressed.

keyUp(modifier_key)

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Purpose: Performs a key release.

Hence with a combination of these two methods, we can capture the special function of a particular key.

Example

public static void main(String[] args)
{
    String baseUrl = "";
    WebDriver driver = new FirefoxDriver();
    
    driver.get("baseUrl");
    WebElement txtUserName = driver.findElement(By.id(“Email”);

    Actions builder = new Actions(driver);
    
    Action seriesOfActions = builder
    .moveToElement(txtUerName)
    .click()
    .keyDown(txtUserName, Keys.SHIFT)
    .sendKeys(txtUserName, “hello”)
    .keyUp(txtUserName, Keys.SHIFT)
    .doubleClick(txtUserName);
    .contextClick();
    .build();
    seriesOfActions.perform();
}

How can you fetch an attribute from an element? How to retrieve typed text from a textbox in Selenium?

We can fetch the attribute of an element by using the getAttribute() method.

Sample code:

WebElement eLogin = driver.findElement(By.name("Login");
String LoginClassName = eLogin.getAttribute("class");

Here, we are finding the web page's login button named "Login". Once that element is found, getAttribute() can be used to retrieve any attribute value of that element and it can be stored it in string format. In my example, We have retrieved "class" attribute and stored it in LoginClassName.

Similarly, to retrieve some text from any textbox, we can use getText() method. In the below piece of code I have retrieved the text typed in the "Login" element.

WebElement eLogin = driver.findElement(By.name("Login");
String LoginText = Login.getText ();
Subscribe to