Frequently asked Selenium Interview Questions


Selenium-Interview-Questions-FAQs



Hi All, Welcome! This article will feature the most commonly asked questions in a Selenium interview. These questions are related to Selenium Webdriver, not the whole suite.





1) Maximize a window - Selenium?      
 
 driver.manage().window().maximize();   



2) Refresh a page - Selenium?
There are three ways of refreshing a page in the browser :

 
 //First  
 driver.navigate().refresh();  
 //Second  
 driver.sendKeys(Keys.F5);  
 //Third  
 driver.get(driver.getCurrentURL());  



3) Navigating forward and backward - Selenium?

navigate() function can be used to navigate between pages.

 
 driver.navigate().forward() : //Navigates forward one page  
 driver.navigate().back() : //Navigates backward one page  



4) Taking Screenshot of a Web Page?

 
 File src =( (TakeScreenShot)driver).getScreenshotAs(OutputType.File);  
 FileHandler.copy(src, new File(“filepath”));  

Note – FileUtils.copy() was used with previous Selenium versions.



5) Handling Alerts – Selenium?

 
 driver.switchTo().alert().accept(); //Equivalent to pressing Yes/Ok in alert  
 driver.switchTo().alert().dismiss(); //Equivalent to pressing No in alert  



6) Find all links in a Web Page?
driver.findElements(By.tagName(“a”)); can be used to find all the links in a web page. It returns a list of anchor(<a>) tags present on the web page.
 
 
 List<WebElement> links = driver.findElements(By.tagName(“a”));  



7) Difference between findElement() and findElements()?

findElement() - Finds a single element in a web page and returns NoSuchElementException when element is not found.

findElements() – Finds a list of element in a web page and returns empty list when elements are not found.



8) Find all links of a particular section in a web page.
If you want to find links in a particular section of a web page, suppose the footer section with id ‘footer’.

 
 WebElement footer = driver.findElement(By.id(“footer”));  
 List<WebElement> links = footer.findElements(By.tagName(“a”));  




9) Moving to a frame - Selenium?
driver.switchTo().frame(<frameId/frameName/frameIndex>); - can be used to switch to a particular frame.

frame() takes either the frameId, frameName, or frameIndex as a parameter.
                   
And to move focus back to the window the following code is used:

 
 driver.switchTo().defaultContent();  



10) Actions Class - Selenium?
Actions class in selenium can be used to perform user actions on a web page which the user performs manually. This class can be used to execute compound user actions simultaneously. For example, if you want to move to an element and then click it or if you want to move to an element then click it and drag it to another container.

Below code drags and drops an element :

 
 Actions action = new Action(driver);  
 action.clickAndHold(sourceWebElement)  
       .moveToElement(targetWebElement)  
       .release(targetWebElement)  
       .build().perform();  



------------------------------------------------------------------------------------------

I will be posting more advanced questions in the upcoming posts. Stay Tuned. Thanks.
Do send your comment if any doubts!