Selenium Interview Question Part – 1
This is my first post in the series of selenium interview preparation, we will learn step by step. Let us first focus on understanding basic selenium concepts.
People with experience less than 3 years can expect these kind of selenium interview question.
Ques 1: What are different get commands in Selenium WebDriver?
- getTitle() – To get the title of the page.
- getCurrentUrl() – To get the current URL of the page.
- getPageSource() – To get the HTML code of a page.
- getText() – To get the text from a WebElement.
- getAttribute() – To fetch the value of an attribute from HTML code of a WebElement.
- getWindowHandle() – This method returns a unique session id assigned to the currently active window.
- getWindowHandles() – This method returns a set of session id’s assigned to all the windows opened.
Ques 2: What are different navigate commands?
- navigate.to(URL) – To navigate to a particular URL.
- navigate.back() – To navigate back to a URL in browsing history.
- navigate.forward() – To navigate forward to a URL in browsing history.
- navigate.refresh() – To reload the page.
Ques 3: What is difference between close() and quit() method?
- The close() method is to close the currently active window of the browser.
- The quit() method is to close all the windows session.
Ques 4: How to launch Internet Explorer and Chrome Driver?
- Launching Internet Explorer Driver:
System.setProperty("webdriver.ie.driver", "<path of IE Driver>"); InternetExplorerDriver IEDriver = new InternetExplorerDriver();
- Launching Chrome Driver:
System.setProperty("webdriver.chrome.driver", "<path of Chrome Driver>"); ChromeDriver ChDriver = new ChromeDriver();
Ques 5: What is the command to delete all (by pass) cookies from a browser?
- deleteAllCookies() method is used to delete all the cookies from a browser.
Ques 6: What are different Identifiers used in Selenium WebDriver?
There are 8 Locator’s or Identifiers in Selenium WebDriver:
Ques 7: How to clear text from a textbox?
- clear() method is used to clear textbox.
Ques 8: What is the return type of findElement() and findElements()?
- findElement() method returns a unique WebElement.
- findElements() method returns a list of all the matching WebElements.
Ques 9: What is the difference between “/” and “//” in xpath ?
- “/” is used to access the immediate child of a tag whereas “//” is used access any child of a Tag.
- “/” is used in absolute xpath and “//” is used in writing relative xpath.
html/body/table/tbody/tr/td/input – This is an example of absolute xpath. we are navigating to input tag by accessing immediate childs.
//input[@type=’text’] – This is an example of relative xpath. It represents firt input tag with type=’text’
Ques 10: How to get the text from a WebElement?
- getText() – This method is used to get the text from a WebElement.
Ques 11: Write a code get number of links on a page?
int numberOfLinks = driver.findElements(By.tagname("a")).size();
Ques 12: How to perform Double click operation using Selenium WebDriver?
- Actions class from Selenium WebDriver has a method doubleClick() which is used to perform double click operation.
Actions action = new Actions(driver); action.doubleClick(element).build.perform();
Ques 13: What is the significance of contextClick() method in Selenium?
- contextClick() method is used to perform right click event of Mouse Operation.
- Actions class from Selenium WebDriver has this method “contextClick()” which performs Right Click.
Actions action = new Actions(driver); action.contextClick(element).build.perform();
Ques 14: Is WebElement an Interface or a Class?
- WebElement is an Interface.
Ques 15: What is the method used to verify whether a checkbox/radio button is checked on not?
- isSelected() – method is used to verify whether a checkbox or a radio is checked or not.
Ques 16: How to verify that a web element is greyed out or not?
- isEnabled() – method is used to verify whether a WebElement is greyed out or not.
Ques 17: How to handle a dropdown in Selenium WebDriver?
- Select class from Selenium WebDriver is used to handle Dropdown.
Select dropdown = new Select(Element);
Now this dropdown object of class Select has below methods to work with on a Dropdown:
- selectByValue(“value”); – Selects an option by value from a dropdown.
- selectByVisibleText(“text”); – Selects an option by visible text from a dropdown.
- selectByIndex(index); – Selects by index from the dropdown.
- deselectByValue(“value”); – Deselects an option by value from a dropdown.
- deselectByVisibleText(“text”); – Deselects an option by visible text from a dropdown.
- deselectByIndex(index); – Deselects an option by index from a dropdown.
- deselectAll(); – Deselects All options in a dropdown.
- isMultiple(); – Boolean operation which verifies whether a dropdown allows multiple selection or not.
- getOptions(); – Returns a list of all options from a dropdown.
- getFirstSelectedOption(); – Returns first option selected in a dropdown as a WebElement .
- getAllSelectedOptions(); – Returns a list of all selected options in a dropdown.
Ques 18: How to perform Drag and Drop operation using Selenium WebDriver?
- Drag and drop operation means to grab an object and move it to some other location. Selenium WebDriver provides Actions class to perform drag and drop operation.
Actions action = new Actions(driver); action.dragAndDrop(sourceElement, destinationElement).build.perform();
- FirefoxDriver is a class in Selenium WebDriver which implements an Interface called WebDriver.
Ques 20: How to validate that a WebElement is visible on a page?
- isDisplayed() is a boolean method which returns whether a WebElement is visible on a page or not.
Ques 21: What is the difference between a getWindowHandle() and getWindowHandles() methods?
- getWindowHandle() – method returns a unique sessionId of the currently active window, which selenium maintains to identify a browser window session.
- getWindowHandles() – method returns a set of unique sessionIds of all the opened windows.
Ques 22: How to switch to a new window (or a new tab) which opens up after you click on a link?
- When a click operation on link results in opening up of a new window (or new tab), the control of selenium does not automatically switches to the new window (or new tab). We have to write code to switch selenium code to the new window.
- Save the sessionId or window handle of Parent window.
- Click on the click which performs opening up of a new window operation.
- Get the sessionId of Child Window.
- Switch to the child window.
// Defining variables to store session id of parent window and child window String sParentWindow, sChildWindow; // Getting session id of parent window sParentWindow = Driver.getWindowHandle(); System.out.println("Session Id od Parent window "+ sParentWindow); //Clicking on a button or link which results in opening up of a new window Driver.findElement(By.tagName("button")).click(); //Getting session Id of child window sChildWindow = Driver.getWindowHandles().toArray().toString(); System.out.println("Session id of Child Window "+ sChildWindow); //Switching to Child Window Driver.switchTo().window(sChildWindow); System.out.println("Title of Child Window is : "+ Driver.getTitle()); Driver.close(); //Switching back to parent window Driver.switchTo().window(sParentWindow);
- HTML UnitDriver, PhantomJS are two famous headless browsers. Now, even chrome has provided a support of headless browser testing.
Ques 24: How to switch back from a frame?
- defaultContent() – is the method to switch back from a frame to the parent window.
Ques 25: Write a code to get URL attribute from a link webElement?
- Links are represented by anchor tag on a web page “<a>”
- Anchor tag has an attribute called href which keeps the value of the URL (address where will navigate to after clicking).
- getAttribute() – method is used to get the attribute from a WebElement.
WebElement link = Driver.findElement(By.linkText("Advertise")); String url = oLink.getAttribute("href"); System.out.println("From attribute :"+ url);