0% found this document useful (0 votes)
15 views15 pages

Interview Qns & Answers

Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

Interview Qns & Answers

Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 01)

1. What is Selenium WebDriver?

Selenium WebDriver is a web automation tool used for automating web application
testing. It allows testers to control a browser programmatically by simulating user
interactions with a web page. WebDriver directly communicates with the browser,
providing faster execution and more control.

2. What are the main advantages of using Selenium WebDriver?

- Open-source and free to use

- Supports multiple browsers like Chrome, Firefox, Safari, and Edge

- Supports various programming languages such as Java, Python, and C#

- Can be integrated with other testing frameworks like TestNG, JUnit, and Cucumber

- Provides efficient support for browser-based regression testing

3. Explain the difference between Selenium WebDriver and Selenium RC.

Selenium WebDriver is faster than Selenium RC, as WebDriver interacts directly with
the browser. Selenium RC requires a server to execute test cases, making it slower and
more complex. WebDriver also supports a more modern, stable API.

4. What are WebDriver’s main features?

- Cross-browser compatibility

- Supports multiple programming languages

- Direct communication with the browser

- Ability to handle dynamic web elements

- Provides flexible locators for elements such as XPath, CSS Selector, and ID

5. Explain different locators in Selenium WebDriver.

- ID: Selects element by the unique ID attribute

- Name: Selects element by the Name attribute

- Class Name: Selects element by Class Name

- Tag Name: Selects element by Tag Name

- Link Text: Selects link element by visible text

- Partial Link Text: Selects link by partial text

- XPath: Locates elements using XML path expressions

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 01)

- CSS Selector: Locates elements based on CSS style selectors

6. What is an XPath in Selenium, and why is it used?

XPath is an XML path language used for navigating elements in an XML document. It is
used in Selenium to locate elements when the element has no unique ID, Name, or Class.
XPath is highly flexible and supports locating elements based on attributes, text, or even
partial matches.

7. What is the difference between absolute and relative XPath?

Absolute XPath starts from the root node and goes to the target element. It begins with a
single slash “/” and is prone to changes in the HTML structure. Relative XPath starts
from the middle of the HTML document and begins with a double slash “//,” which is
more stable as it doesn’t depend on the entire hierarchy.

8. How do you launch a browser in Selenium WebDriver?

In Selenium WebDriver, the driver instance for a specific browser (like ChromeDriver or
FirefoxDriver) is created to launch a browser. For example, `WebDriver driver = new
ChromeDriver();` launches the Chrome browser.

9. How do you perform browser navigation in WebDriver?

The `navigate()` method in WebDriver allows for browser navigation. For instance:

- `driver.navigate().to("url")` to go to a specific URL

- `driver.navigate().back()` to go back in browser history

- `driver.navigate().forward()` to go forward in browser history

- `driver.navigate().refresh()` to refresh the page

10. How can you handle alerts in Selenium WebDriver?

To handle alerts, WebDriver provides the `switchTo().alert()` method. It has several


functions, such as:

- `accept()` to click on the OK button

- `dismiss()` to click on the Cancel button

- `getText()` to retrieve the text from the alert box

- `sendKeys("text")` to send text to the alert box

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 01)

11. Explain how to handle multiple windows in Selenium WebDriver.

WebDriver provides `getWindowHandles()` to get the IDs of all open windows and
`switchTo().window(windowID)` to switch control to a particular window. Each
browser window has a unique window handle, which can be iterated over to manage
multiple windows.

12. What are WebDriver waits, and what types are available?

WebDriver offers two types of waits:

- Implicit Wait: Sets a global wait time for all elements. WebDriver waits until the
specified time before throwing a NoSuchElementException.

- Explicit Wait: Applies to specific elements, allowing for more targeted waiting
conditions (such as element visibility or clickability).

13. Explain the Stale Element Reference Exception.

This exception occurs when an element is no longer present in the DOM. This can
happen when the DOM structure changes, such as during page navigation or dynamic
content updates. Solutions include re-locating the element or using wait statements to
wait for the element to reappear.

14. What is the difference between findElement and findElements?

`findElement` locates the first element matching the locator and returns it, throwing an
exception if no element is found. `findElements` returns a list of all elements matching
the locator, returning an empty list if no elements are found.

15. How can you handle frames in Selenium WebDriver?

The `switchTo().frame()` method allows switching control to an iframe. Frames can be


accessed by index, name, or WebElement reference. To switch back to the main
document, `switchTo().defaultContent()` is used.

16. How do you capture screenshots in Selenium WebDriver?

Screenshots can be captured by casting WebDriver to TakesScreenshot, then calling


`getScreenshotAs()` to save the screenshot to a file.

17. How can you perform drag and drop in Selenium WebDriver?

The Actions class in WebDriver can be used for drag-and-drop operations. It has
methods like `clickAndHold`, `moveToElement`, and `release` to perform drag-and-drop
actions.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 01)

18. Explain what Actions class is and its use.

The Actions class in Selenium is used to handle advanced user interactions, such as drag
and drop, keyboard actions, and mouse movements. It provides methods like
`moveToElement`, `clickAndHold`, `doubleClick`, and `contextClick`.

19. How do you scroll a web page in Selenium WebDriver?

JavaScriptExecutor is used to perform scroll actions. It allows executing JavaScript code


directly in the browser. For example, to scroll down, `((JavascriptExecutor)
driver).executeScript("window.scrollBy(0,500)");` can be used.

20. How do you handle dropdowns in Selenium WebDriver?

The Select class is used for handling dropdown elements. It provides methods like
`selectByVisibleText`, `selectByIndex`, and `selectByValue` for selecting dropdown
options.

21. Explain the Page Object Model and its advantages.

Page Object Model (POM) is a design pattern that enhances code maintainability and
reusability by representing each web page as a separate class containing web elements
and actions. POM reduces code duplication and makes the code cleaner and more
organized.

22. What is a Fluent Wait in Selenium WebDriver?

Fluent Wait allows defining the maximum wait time, polling frequency, and ignoring
specific exceptions while waiting. It repeatedly checks the condition and provides more
flexibility than implicit and explicit waits.

23. What is TestNG, and why is it used with Selenium?

TestNG is a testing framework used for structuring and managing test cases in
Selenium. It provides features like parallel execution, grouping of test cases, data-driven
testing, annotations, and generating reports.

24. How can you perform data-driven testing in Selenium?

Data-driven testing can be achieved using the TestNG DataProvider or by reading data
from external files (Excel, CSV, etc.) using libraries like Apache POI. It allows running
the same test case with multiple sets of input data.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 01)

25. Explain what cross-browser testing is and how to perform it in Selenium.

Cross-browser testing ensures that the web application works consistently across
different browsers. In Selenium, cross-browser testing is performed by specifying the
browser drivers (like ChromeDriver, FirefoxDriver) and executing the same script on
multiple browsers.

26. How can you manage cookies in Selenium WebDriver?

In Selenium WebDriver, cookies can be managed using methods like `addCookie()` to


add a new cookie, `getCookieNamed()` to retrieve a specific cookie, `deleteCookie()` to
delete a particular cookie, and `deleteAllCookies()` to remove all cookies. Cookies help
in testing scenarios where session handling is required.

27. Explain how you would handle a dynamically loaded element.

To handle dynamically loaded elements, it’s best to use explicit waits, like
WebDriverWait with ExpectedConditions. This allows waiting until a specific condition
is met (like element visibility or clickability) before proceeding, ensuring the element is
available.

28. What is the use of JavaScriptExecutor in Selenium WebDriver?

JavaScriptExecutor is used to execute JavaScript commands within the browser. It is


often used for actions that WebDriver cannot perform directly, such as scrolling,
clicking hidden elements, or retrieving the page title. JavaScriptExecutor can be cast
from WebDriver and used to run scripts within the browser.

29. How can you switch between tabs in Selenium WebDriver?

Switching between tabs can be managed using `getWindowHandles()` to obtain all


window handles, and `switchTo().window(windowHandle)` to switch to the desired tab
by its handle. Tabs in the same browser instance are treated similarly to multiple
windows.

30. Explain how to verify if an element is displayed, enabled, or selected.

WebDriver provides methods to check element states:

- `isDisplayed()` checks if an element is visible on the web page

- `isEnabled()` verifies if an element is enabled and available for interaction

- `isSelected()` checks if an element, usually a checkbox or radio button, is selected

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 01)

31. What is an iframe, and how do you handle iframes in Selenium?

An iframe is an HTML document embedded inside another HTML document. To interact


with elements inside an iframe, WebDriver must switch to it using `switchTo().frame()`,
which accepts the iframe's index, name, or WebElement reference.

32. How can you close all browser windows opened by Selenium?

To close all open browser windows, the `driver.quit()` command is used. This
terminates all active sessions and closes all associated browser windows.

33. How do you handle a file upload in Selenium WebDriver?

For file uploads, WebDriver can use the `sendKeys()` method to pass the file path
directly to the file input element (typically with a locator for the input field with
`type="file"`). This approach is simple and does not require additional tools.

34. How do you handle a file download in Selenium?

File downloads are generally handled by setting browser preferences (e.g., setting
default download directory) before initiating a download in Selenium. For instance, in
Firefox, preferences can be set using FirefoxOptions, which specify download behavior.

35. What is the difference between driver.close() and driver.quit()?

`driver.close()` closes the current browser window where the focus is present, while
`driver.quit()` closes all browser windows and terminates the WebDriver session.

36. How can you maximize the browser window in Selenium WebDriver?

To maximize the browser window, WebDriver provides


`driver.manage().window().maximize()` which sets the browser window to full screen.

37. What is a WebDriverWait, and why is it used?

WebDriverWait is an explicit wait that applies to a specific condition. It is used to wait


for certain conditions (like element visibility, clickability) before proceeding, which
helps in handling dynamic web elements and avoiding exceptions.

38. How do you perform right-click actions in Selenium?

Right-click actions can be performed using the Actions class with the `contextClick()`
method. For example, `actions.contextClick(element).perform()` performs a right-click
on the specified element.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 01)

39. Explain how to handle browser pop-ups in Selenium.

For browser pop-ups like alerts, WebDriver can handle them using `switchTo().alert()`,
with methods like `accept()` to click OK, `dismiss()` to cancel, and `getText()` to read the
pop-up text.

40. How can you perform double-click actions in Selenium?

The Actions class in Selenium provides `doubleClick()` to perform double-click actions


on elements. It is useful for interacting with elements that require a double-click.

41. What is the use of getText() in Selenium?

`getText()` retrieves the visible text of a web element, often used for verification
purposes to check if the displayed text on the webpage matches the expected text.

42. How can you run Selenium tests in parallel?

In TestNG, parallel test execution can be configured in the `testng.xml` file by setting the
`parallel` attribute to methods, classes, or tests. This reduces overall execution time by
running multiple tests concurrently.

43. Explain the difference between Assert and Verify in Selenium.

`Assert` stops the test execution if the condition fails, while `Verify` checks the condition
but allows the test to continue even if the verification fails. `Assert` is commonly used
when the test cannot proceed without a certain condition being met, whereas `Verify` is
used for optional checks.

44. What is the difference between CSS Selector and XPath in Selenium?

CSS Selector is generally faster and less complex than XPath, making it preferable for
simple element locating. XPath is more flexible and allows navigating in both directions
(parent-child, child-parent) in the DOM, which CSS Selector does not support.

45. How do you generate reports in Selenium?

Reports can be generated using TestNG's built-in reporting feature, or by integrating


third-party tools like Extent Reports or Allure, which provide more detailed and
customizable reports on test execution.

46. What are the benefits of using the Page Factory in Selenium?

Page Factory in Selenium is used to initialize web elements defined in the Page Object
Model. It simplifies element locating by using annotations like `@FindBy`, and
`initElements` to initialize elements, leading to cleaner and more maintainable code.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 01)

47. Explain how to create and handle custom ExpectedConditions in Selenium.

In WebDriver, custom ExpectedConditions can be created by implementing the


`ExpectedCondition` interface. This allows you to define unique conditions that
standard ExpectedConditions don’t cover, enabling more flexibility with waits.

48. How do you handle SSL certificate errors in Selenium WebDriver?

To handle SSL certificate errors, browser capabilities can be configured. For instance, in
Chrome, `ChromeOptions` has the `acceptInsecureCerts` option which can be set to true
to bypass SSL warnings.

49. What is headless browser testing, and why is it used?

Headless browser testing allows test execution without a visible browser UI. It is faster
and consumes less memory, suitable for environments where visual UI is unnecessary.
Headless mode can be set in Chrome, Firefox, and other browsers.

50. How can you execute JavaScript directly in Selenium WebDriver?

JavaScript can be executed using the JavaScriptExecutor interface in Selenium. By


casting WebDriver to JavaScriptExecutor, you can run JavaScript commands with the
`executeScript()` method, useful for actions like clicking hidden elements, scrolling, or
retrieving hidden text.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 02)

51. How can you handle AJAX calls in Selenium WebDriver?

AJAX calls are asynchronous, so WebDriver needs to wait for the data to load. Explicit
waits, such as WebDriverWait with ExpectedConditions (e.g.,
`visibilityOfElementLocated` or `presenceOfElementLocated`), are helpful in handling
AJAX elements and ensuring the DOM updates before proceeding.

52. What is the purpose of DesiredCapabilities in Selenium?

DesiredCapabilities is used to set properties for WebDriver instances, such as browser


name, version, platform, and more. It is often used in cross-browser testing to define
specific browser configurations and is especially useful in remote testing with Selenium
Grid.

53. How do you execute tests on a remote machine using Selenium Grid?

To execute tests on a remote machine, configure a Selenium Grid with a Hub and
multiple Nodes. The Hub receives the test commands, and the Nodes execute the tests.
By setting the remote WebDriver URL to the Hub’s address and passing the desired
capabilities, tests run on specified browser and OS combinations.

54. What is the purpose of the fluent wait in Selenium WebDriver?

Fluent wait is a type of explicit wait that allows defining the maximum time to wait for a
condition, the frequency to check for the condition, and the ability to ignore specific
exceptions. Fluent wait is useful when dealing with elements that require dynamic
waiting and custom handling.

55. How do you perform cross-browser testing in Selenium?

Cross-browser testing in Selenium is achieved by setting up WebDriver for different


browsers (e.g., ChromeDriver, FirefoxDriver) or by using DesiredCapabilities in
Selenium Grid. By running tests on multiple browsers, it ensures the application works
consistently across them.

56. What is the difference between `findElement()` and `findElements()`?

`findElement()` locates a single element based on the specified locator and throws a
`NoSuchElementException` if not found. `findElements()` returns a list of matching
elements and returns an empty list if no elements are found, allowing for safer handling.

57. How can you handle multiple child windows in Selenium WebDriver?

Multiple child windows can be handled by using `getWindowHandles()` to get a set of all
window handles and iterating over them to switch to each window using
`switchTo().window(handle)`. This is often used for popup handling or switching
between multiple browser tabs.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 02)

58. Explain the difference between Absolute and Relative XPath.

Absolute XPath starts from the root node (using a single slash '/') and navigates through
the entire DOM hierarchy. Relative XPath starts from any point in the DOM (using
double slashes '//') and is preferred for its flexibility and reliability, especially in
dynamic pages.

59. What are the common exceptions encountered in Selenium WebDriver?

Some common exceptions include:

- `NoSuchElementException`: Element not found in the DOM

- `StaleElementReferenceException`: Element is no longer attached to the DOM

- `ElementNotVisibleException`: Element is not visible

- `TimeoutException`: Command execution timeout

- `WebDriverException`: General error in WebDriver

60. What is the purpose of the `navigate().to()` method in Selenium?

`navigate().to()` is used to open a new URL, similar to `get()`, but with additional
navigation control. It allows performing forward and backward navigation through the
browser history with methods like `navigate().back()` and `navigate().forward()`.

61. How do you handle a dropdown in Selenium WebDriver?

Dropdowns in Selenium are handled with the `Select` class, which provides methods
like `selectByVisibleText`, `selectByValue`, and `selectByIndex` to choose options.
`deselectAll()` and other methods can also be used for multi-select dropdowns.

62. How can you validate page titles in Selenium?

The `getTitle()` method is used to retrieve the page title. Validating the title involves
checking if the title text matches the expected text, often done with assertions to
confirm navigation correctness.

63. Explain implicit wait and where it’s used.

Implicit wait is a global wait that applies to all elements and commands in the
WebDriver session. It waits for a specified time before throwing a
`NoSuchElementException`. It is used when you want to set a default wait time for
locating elements.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 02)

64. What is the purpose of `driver.switchTo().defaultContent()` in Selenium?

`driver.switchTo().defaultContent()` is used to switch back to the main content of the


page from an iframe or any nested frame. This is required after performing actions
inside a frame and returning to the main content.

65. How can you capture a screenshot in Selenium WebDriver?

To capture a screenshot, WebDriver provides the `TakesScreenshot` interface with


`getScreenshotAs()` method, which saves the screen image to a file. Screenshots are
helpful for debugging and reporting test results.

66. What is the purpose of `driver.manage().timeouts().pageLoadTimeout()` in


Selenium?

`pageLoadTimeout()` sets the maximum time to wait for a page to load. If the page takes
longer than the specified time, a TimeoutException is thrown. It’s useful in handling
scenarios where page load speed may vary.

67. How can you maximize the browser window in Selenium WebDriver?

The `driver.manage().window().maximize()` command maximizes the browser window


to the full screen size. This is useful to ensure the page elements are fully visible during
testing.

68. Explain the use of @FindBy annotation in Selenium.

`@FindBy` is part of the Page Factory in Selenium. It is used to locate elements using
different locators like id, name, class, and XPath. It simplifies locating elements by
directly binding them to variables, making code more readable.

69. What is the purpose of using `driver.getCurrentUrl()`?

`driver.getCurrentUrl()` retrieves the URL of the current page. This is often used in
verification steps to ensure the user is on the correct page after navigation or
redirection.

70. How do you switch to an alert in Selenium?

To switch to an alert, use `driver.switchTo().alert()`. This returns an Alert object,


allowing methods like `accept()` and `dismiss()` to interact with it.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 02)

71. How can you upload a file without using sendKeys()?

To upload a file without `sendKeys()`, third-party tools like AutoIt, Robot Class, or
JavaScriptExecutor can be used. These tools help handle native file upload dialogs that
Selenium cannot directly interact with.

72. Explain the role of WebDriverManager in Selenium.

WebDriverManager is a tool that automatically downloads and configures the correct


WebDriver binaries for the browser version. It eliminates the need for manual
WebDriver setup, making tests more reliable across environments.

73. How can you disable browser notifications in Selenium WebDriver?

Browser notifications can be disabled by setting browser-specific preferences or


capabilities. For instance, in Chrome, you can set `ChromeOptions` to disable
notifications by using the `--disable-notifications` argument.

74. Explain how to handle StaleElementReferenceException in Selenium.

`StaleElementReferenceException` occurs when an element is no longer attached to the


DOM. To handle it, you can refresh the element’s reference, retry the action, or use
explicit waits to ensure the element is stable.

75. What is the purpose of setting browser options in Selenium?

Browser options like ChromeOptions or FirefoxOptions allow configuring browser


behavior (e.g., disabling notifications, running in headless mode, setting download
paths). Setting options improves test stability and ensures consistent behavior across
runs.

76. What is the purpose of WebDriverWait in Selenium?

WebDriverWait is an explicit wait that waits for a specified condition to be met before
executing further commands. It helps in handling dynamic elements that may load or
change state unpredictably.

77. How do you handle pop-ups in Selenium WebDriver?

To handle pop-ups, use `Alert` interface methods like `accept()` to confirm or `dismiss()`
to cancel the pop-up. For browser-based pop-ups, switching to the alert using
`driver.switchTo().alert()` is necessary.

78. How can you perform drag-and-drop actions in Selenium WebDriver?

Drag-and-drop actions are performed using the `Actions` class with methods like
`clickAndHold(source)`, `moveToElement(target)`, and `release()`. The
`dragAndDrop(source, target)` method also simplifies this process.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 02)

79. How do you handle mouse hover actions in Selenium?

Mouse hover actions are achieved using the `Actions` class with
`moveToElement(element)`. This simulates moving the mouse over the element,
triggering any hover effects.

80. How can you execute JavaScript in Selenium WebDriver?

JavaScript execution is done using the `JavascriptExecutor` interface. This is useful for
actions like scrolling, clicking hidden elements, or fetching browser details not
accessible through WebDriver methods.

81. What is Page Object Model (POM) in Selenium?

POM is a design pattern that promotes better test structure by creating separate classes
for each page. It provides a clear representation of elements and actions on each page,
making tests more maintainable.

82. Explain the Page Factory in Selenium.

Page Factory is a class in Selenium that extends the POM concept. It uses `@FindBy`
annotations to initialize elements when an instance of the page class is created,
reducing code and improving readability.

83. How do you handle cookies in Selenium WebDriver?

Cookies can be handled using methods like `getCookies()` to retrieve all cookies,
`addCookie()` to add a cookie, and `deleteAllCookies()` to remove all cookies. Managing
cookies helps in maintaining user sessions.

84. How do you scroll in Selenium WebDriver?

Scrolling can be done using `JavascriptExecutor` with commands like


`executeScript("window.scrollBy(0,250)");`. WebDriver does not have direct scrolling
commands, so JavaScript is used for these interactions.

85. How do you handle SSL certificates in Selenium?

SSL certificate errors can be managed using ChromeOptions or FirefoxOptions by


setting capabilities like `acceptInsecureCerts` to `true`. This is useful when testing sites
with invalid SSL certificates.

86. What is the use of `driver.manage().deleteAllCookies()` in Selenium?

`driver.manage().deleteAllCookies()` removes all cookies for the current session. It is


useful for testing fresh sessions without retaining previous data like login credentials.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 02)

87. Explain the concept of headless browser testing in Selenium.

Headless browser testing is executing tests without a visible browser UI. Options like
Chrome and Firefox support headless mode, which reduces resource usage and speeds
up testing.

88. How do you perform keyboard actions in Selenium?

Keyboard actions are done using the `Actions` class and methods like
`sendKeys(Keys.ENTER)`. This helps simulate keyboard inputs such as pressing keys or
combinations.

89. How can you handle dynamic elements in Selenium WebDriver?

Dynamic elements can be handled by using explicit waits like `WebDriverWait` to wait
for specific conditions, such as element visibility or presence, before interacting with
them.

90. Explain the purpose of `driver.switchTo().frame()` in Selenium.

`driver.switchTo().frame()` is used to switch WebDriver’s context to an iframe or frame.


This is necessary to interact with elements inside a frame, as WebDriver cannot directly
access elements within frames.

91. What is the purpose of `driver.navigate().refresh()` in Selenium?

`driver.navigate().refresh()` reloads the current page, ensuring the latest content is


loaded. It’s useful in scenarios where page content may change or to retry loading
dynamic elements.

92. How do you handle broken links in Selenium?

Broken links can be identified by retrieving each link’s URL, making an HTTP request
using tools like `HttpURLConnection` in Java, and checking if the response status is 200
for valid links.

93. How can you verify an image is displayed in Selenium?

To verify if an image is displayed, use `isDisplayed()` to check its visibility. For further
validation, fetch the image URL and check the HTTP status code to ensure it loads
properly.

94. How do you switch between browser tabs in Selenium WebDriver?

Switch between tabs by using `getWindowHandles()` to retrieve all window handles


and `switchTo().window(handle)` to change the context to the desired tab.

PDF prepared by Ram Sharan Follow me on Linkedin


100 SELENIUM INTERVIEW QUESTIONS AND ANSWERS (PART 02)

95. How do you perform double-click actions in Selenium?

Double-click actions are achieved using the `Actions` class with the
`doubleClick(element)` method. This is useful for elements requiring double-click
interactions.

96. How can you get the page source in Selenium WebDriver?

The `getPageSource()` method retrieves the entire HTML source of the current page.
This is helpful for validating if specific content is present or for debugging purposes.

97. Explain the use of `driver.switchTo().parentFrame()` in Selenium.

`driver.switchTo().parentFrame()` switches the context back to the parent frame from a


nested frame. It’s helpful when you need to interact with elements outside the current
frame.

98. What is the purpose of `driver.getWindowHandle()`?

`driver.getWindowHandle()` retrieves the handle of the current window. This handle is


unique to each window, helping in switching back to this specific window after
navigating to other windows or tabs.

99. How can you handle a file download in Selenium WebDriver?

File downloads can be handled by setting download preferences in browser options,


such as specifying the download path or disabling the download prompt. For instance,
ChromeOptions allows configuring default download settings.

100. How do you implement test retries in Selenium?

Test retries can be implemented using test frameworks like TestNG or JUnit, which
support retry mechanisms in case of test failures. This approach helps automatically
rerun failed tests, improving test reliability.

PDF prepared by Ram Sharan Follow me on Linkedin

You might also like