Selenium Interview Questions
Selenium Interview Questions
Selenium Interview Questions
Table of Contents
Locators
1. Different types locators that can be used locate elements in Selenium
A. You can locate elements in Selenium using By class. Following are the locators in By
class you can use:
1. name
2. className
3. linkText
4. id
5. xpath
6. cssSelector
7. partialLinkText
8. tagName
9. js (new method)
3. If there are multiple tagnames and you do not specify attribute, Selenium identifies the
first one
A. <input class = ”username” id=”email” value=”text”>
<input class = ”password” id=”pass” value=”text”>
B. If you try selecting using only tagname and value, selenium will return the first found
element, which is input tag for username.
C. Selenium scans from top left for each tag
4. When xpath starts with html-Not reliable- Switch browser to get another one
A. Sometimes, browser returns xpath from html/body/div/…
B. Such xpaths are not usually accurate, since it may break when developer makes any
change to the html
13. How to traverse back to Parent element from Child element using Xpath?
A. Example:
.//*[@id='tablist1-tab1']/parent::ul
<ul class=”responsive-tabs_list” role=”tablist”> <- Returns this node
<li id=”tablist1-tab”> Item1 </li>
<li id=”tablist1-tab2”> Item2 </li>
<li id=”tablist1-tab3”> Item3 </li>
</ul>
isEnabled()
-Is the element currently enabled or not? This will generally return true for everything but
disabled input elements.
Page |7
isSelected()
-Determine whether or not this element is selected or not. Often used with checkboxes
submit()
-If this current element is a form, or an element within a form, then this will be submitted to the
remote server.
Alerts
18. Alerts
We can handle alerts by using switchto() method of the WebDriver (object)
WebDriver.switchto().alert()
Methods that can be run:
To press ok or accept etc. we use accept()
WebDriver.switchto().alert().accept();
To dismiss, quit or exit an alert, we use method dismiss()
WebDriver.switchto().alert().dismiss();
To get the text of the alert, we use getText()
WebDriver.switchto ().alert().getText();
To input text, we use sendKeys()
WebDriver.switchto().alert().sendKeys(String keysToSend);
Synchronization (Waits)
19. Different types of waits
• Implicit wait
• Explicit wait
• Fluent wait
• Thread.sleep
void perform()
- A convenience method for performing the actions without calling build() first.
29. Where and when can you use Selenium’s javascript executor?
Javascript executor method is useful when you cannot get dynamic elements through
normal Selenium methods. The html elements are either hidden or not accessible directly by
selenium. More prominent example where you need to use Javascript executors are when
P a g e | 11
you want to retrieve Autosuggestion text box’s suggestions. Since these suggestions are
displayed only when user partially types input and are thus generated dynamically, it is often
not possible to get the locator of the suggestion text. When you try to inspect the suggestion
elements, they just often disappear. In such cases, you can use javascript executor methods
to read the elements using DOM (instead of Selenium).
Note: If you try to save the file to C: (eg. C:// screenshot.png) you will get Access
violation error. So create a directory under C: drive or save it in some sub directory
under C: