Selenium Questions Answers
Selenium Questions Answers
Selenium Questions:
get() navigate().to()
navigate().to() method is used to open
get() method is used to open the specific url
specific url and also to navigate from one url
in browser.
to another url.
It will till all the elements in webpage are It will not wait till all the elements in
loaded. webpage are loaded.
navigate().back():
Used to navigate back in browser.
Syntax:
driverObjName.navigate().back (); ------ Void
navigate().forward():
Used to navigate forward in browser.
Syntax:
driverObjName.navigate().forward (); ------ Void
navigate().refresh():
Used to refresh the browser page.
Syntax:
driverObjName.navigate().refresh (); ------ Void
2
getText() getAttribute()
getText() method is used to capture the getAttribute() method is used to capture the
visible text present on the particular inner HTML attribute value of that particular
element. element.
We did not pass any parameter in this In this method we need to pass the parameter
method. as attribute key.
Syntax: Syntax:
WebEleName.getText(); WebEleName.getAttribute("attribute Key");
driver.close() driver.quit()
close() method shall close the browser quit() method closes all the browser windows
window which is in focus. that are opened in current execution.
close() method closes the active WebDriver quit() method closes all the active WebDriver
instance. instances.
Syntax: driverObjName.close(); Syntax: driverObjName.quit();
7. How to handle dropdown elements with select tag and without select tag.
We can handle drop down in selenium with the help of Select class.
First we have to create object of Select class by passing drop down
WebElement instance.
Then by using that object we can handle the dropdown.
To select option from drop down we can use three methods
selectByIndex() ------ insert index integer.
selectByValue() ------ insert value in String.
selectByVisibleText() ------ insert visible text in String.
To count the options in drop down we use getOptions() method the return type of
this method is List of WebElement. i.e. List<WebElement>
Then by using size() method we can count the options. The return type of
this method is integer, so we can store the count number in int and then we can
print the count of options present in drop down.
To print all options in drop down we use getOptions() method the return type of
this method is List of WebElement. i.e. List<WebElement>
Then by using Enhance for loop or for loop we can print all options in
dropdown.
To handle dropdown without Select class we can use Actions class method click() and also we
can use WebElement method click().
Frames:
Frames are used to divide browser window i.e. webpage into multiple sections, where
each sections can load a separate HTML page.
There are top frame and child frame present in a webpage.
Whenever we access that particular webpage then the focus is on top frame. We are on
top frame then we have to switch to child frame.
We cannot switch child frame to child frame. First we have to switch to top frame then
we can switch to other child frame.
We can switch to child frame by using three ways,
By using frame index id
Syntax:
driverObjName.switchTo().frame(int index);
Windows:
We can handle tabs/ window by using getWindowHandle() method.
Return type getWindowHandle() method is String.
Then by using if statement we can switch to another tab. This method is only helpful for 2 tabs.
For multiple tab handling we convert set of string into array list and then by using the index
position we can switch to any tab / window.
(then tell the syntax of if statement and array list as in methods.)
In 2nd way we convert set of string into array list and then by using the index position we
can switch to any tab / window. So for parent window the index is always zero (0). So by
passing this zero index in driver.switchTo().window() we can switch to the parent window.
11. How to click or send value to an element without using element.click() and
element.sendKeys()?
We can click or send value to an element by using Actions class methods.
Which are click() method, sendKeys() method, keyDown() method and keyUp() method.
Absolute xpath identifies element fastly than Relative xpath can identify element slowly
relative xpath. than Absolute xpath.
Eg.
Eg. //table/tbody/tr/th
/html/head/body/form/table/tbody/tr/th
tagName: present in html code. Starting text of path of that particular element.
Attribute: it is the key value pair in html code. Like name, class, id, etc.
Value: it is the value of specific attribute key.
NoSuchFrameException:
When WebDriver is trying to switch to an invalid frame,
NoSuchFrameException under NotFoundException class is thrown.
NoSuchWindowException:
NoSuchWindowException comes under NotFoundException class. This is
thrown when WebDriver tries to switch to an invalid window.
NoAlertPresentException
NoAlertPresentException under NotFoundException is thrown when
WebDriver tries to switch to an alert, which is not available.
StaleElementReferenceException:
This exception says that a web element is no longer present in the web
page.
TimeoutException:
The TimeoutException occurs when the command that is currently in
execution and does not completed within the expected time frame.
ElementNotSelectableException:
This exception comes under InvalidElementStateException class.
ElementNotSelectableException indicates that the web element is present in the
web page but cannot be selected.
ElementNotVisibleException:
This exception comes under InvalidElementStateException class.
ElementNotVisibleException occurs when WebElement is present but it not
visible.
NoSuchAttributeException:
NoSuchAttributeException occurs when the attribute of the element
could not be located.
Single slash is used for absolute xpath. Double slash is used for relative xpath.
A single slash ‘/’ anywhere in xpath signifies A double slash ‘//’ signifies to look for any
to look for the element immediately inside its child or any grand-child (descendant)
parent element. element inside the parent element.
16. What are the scenarios that cannot be automated using selenium?
There are many things that cannot be done using Selenium WebDriver. Few of them
which I can list down are as follows:
Bitmap comparison is not possible using Selenium WebDriver.
Automating Captcha is not possible using Selenium WebDriver.
We cannot read bar code using Selenium WebDriver.
We cannot automate OTP submission using Selenium WebDriver.
There are more things that cannot be automated using Selenium WebDriver.
objAct.contextClick(WebElement instance).build().perform();
doubleClick():
Double click on the middle of the given element.
Return type of doubleClick() method is Actions class.
This method is overloaded in Actions class.
Syntax:
objActName.doubleClick(wbName).build().perform();
contextClick():
Right click on the middle of the given element.
Return type of contextClick() method is Actions class.
This method is overloaded in Actions class.
Syntax:
objActName.contextClick(wbName).build().perform();
clickAndHold():
Clicks on the middle of the element and holds up to release.
Return type of clickAndHold() method is Actions class.
This method is overloaded in Actions class.
Syntax:
objActName.clickAndHold( wbName target).build().perform();
dragAndDrop():
Drags the source element and drops at targeted element.
Return type of dragAndDrop() method is Actions class.
This method is overloaded in Actions class.
Syntax:
objActName.dragAndDrop( wbName source, wbName target).build().perform();
moveToElement():
10
Moves the mouse middle of the given element. Mouse over event.
Return type of moveToElement() method is Actions class. This method is overloaded
Syntax:
objActName.moveToElement( wbName target) .build().perform();
release():
Release the pressed mouse button on at current mouse location.
Return type of release() method is Actions class. This method is overloaded
Syntax:
objActName.release().build().perform();
Keyboard event:
keyDown():
It is used to press the given key.
Return type of keyDown() method is Actions class. This method is overloaded.
Syntax:
objActName.keyDown(Keys.KEYNAME) .build().perform();
keyUp():
It is used to release the given key.
Return type of keyUp() method is Actions class. This method is overloaded.
Syntax:
objActName.keyUp(Keys.KEYNAME) .build().perform();
sendKeys():
It is used to enter data in the active element.
Return type of keyUp() method is Actions class. This method is overloaded.
Syntax:
objActName.sendKeys(“Value to send”) .build().perform();
This exception says that a web element is no longer present in the web page.
This exception is not the same as ElementNotVisibleException.
StaleElementReferenceException is thrown when an object for a particular web element
was created in the program without any problem and however; this element is no longer
present in the window. This can happen if there was a
Navigation to another page
DOM has refreshed
A frame or window switch
Example:
WebElement firstName = driver.findElement(By.id(“firstname”));
driver.switchTo().window(Child_Window);
element.sendKeys(“Aaron”);
In the code above, object firstName was created and then the window was switched.
Then, WebDriver tries to type ‘Aaron’ in the form field.
In this case StaleElementReferenceException is thrown.
Example:
Say ‘id’ of a username field is ‘username_1’ and the xpath will
be //*[@id=’firstname_1?].
When you open the page again the ‘id’ might change say to ‘’firstname _11’.
In this case, the test will fail because the WebDriver could not find the element.
and StaleElementReferenceException will be thrown.
We can launch headless browser / script with the help of ChromeOptions class.
First we have to create object of ChromeOptions class.
Then we use addArguments() method and under the argument we pass “--headless”
Then we pass the ChromeOptions object name in ChromeDriver constructor.
We write above script first.
Syntax:
System.setProperty("webdriver.chrome.driver", "Path of chrome driver");
opt.addArguments("--headless");
There are several interface present in the Selenium. Some of them are as follows,
SearchContext is the parent interface of all interface present in Selenium.
WebDriver,
WebElement,
OutputType
Navigation
Options
Timeouts
Window
TargetLocator
TakesScreenshot
findElement() findElements()
build() perform()
build() method is used to combine multiple perform() method is used to execute each
actions in single statement. and every statement / action.
return type of build() method is Action class return type of perform() method is void
abstract method
get() void
getTitle String
getCurrentUrl String
getPageSource() String
close() void
quit() void
Window class:
maximize() void
minimize() void
setSize() void
Dimension class
cons with 2 int arument/parameter width and height
driver.manage().window().setSize(d) void
isDisplayed() boolean
isEnabled() boolean
isSelected() boolean
click() void
sendKeys() void
getText() String
getAttribute() String
clear() void
Link on any web page are given anchor tag name i.e. “a”.
By using tag name locator as “a” and findElements() method we can find the
total links on webpage the return type of findElements() method is list of WebElement.
Then by using size() method we can get the count of links, the return type of
size() method is integer. So we can store it in integer and print it in output console.
Then by using enhance for loop we can print all links in output.
List<WebElement> ls =driver.findElements(By.tagName("a"));
int a =ls.size();
2. dismiss():
When we want to click on cancel button on popup we use dismiss() method.
Return type of dismiss () is void.
Syntax:
altName.dismiss();
3. getText():
If we want to capture visible text present on popup we use getText() method.
Return type of getText() is String.
Syntax:
altName.getText();
4. sendKeys():
If we want to enter any data in popup text box we use sendKeys() method.
Return type of sendKeys() is Void.
Syntax: altName.sendKeys ();
32. How to handle Static table?
16
For following methods first we use findElement() method to find the particular
table, the return type of findElement() is WebElement. So we store it in
WebElement instance and then we use that instance.
Syntax:
WebElement wbTable =driver.findElement(By.id("Employee"));
2) Conditional Synchronization:
Conditional synchronization does not used for all commands/ statement in the
program.
It works only for findElement() and findElements() method only.
There are two types in conditional sync
i) ImplicitlyWait:
The implicitlyWait in selenium WebDriver is used to tell web driver to wait
certain amount of time before throws a NoSuchElementException.
Once we set the time then WebDriver will wait for element before throwing the
exception.
It also known as global wait, it is applicable for all the element in web page
Return type of implicit wait is TimeOuts interface
Method overloading
Syntax:
driver.manage().timeouts().implicitlyWait(timeout,TimeUnit.SECONDS);
ii) ExplictWait:
18
The explicit wait in selenium is used to tell web driver to wait certain condition
(ExpectedConditions) or maximum time exceed before throwing an exception.
It is apply for single element/ object in webpage.
Once we declare explicit wait we have use expectedConditions .
Method overloading.
First we have create object of WebDriverWait by passing WebDriver instance
and time in seconds
Syntax:
WebDriverWait wait =new WebDriverwait(driver,30);
wait.until(ExpectedCondition.visibilityOfElement(By.id("")));
3) Fluent wait:
It is used to change the repetition cycle of finding element.
By default the cycle is repeated in 255 milliseconds
syntax:
Wait wait =new FluentWait(driver)
.withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(Exception.class)
2)operation on button
i)check displayed status isDisplayed();
ii)check enabled status isEnabled();
iii)return the button value getAttribute('locator');
iv)click on button click();
3)operation on link
19
6) operation on images:
there are 3 types of image
1) general image
> check displayed
2) image button
>> check displayed
> enabled status
> click
> capture the image button value
3) image link
> displayed status
> enbled status
> click on image link
8) operation on table
20
constructors
actions
select
dimension