Your content is already well-structured. Here are a few technical corrections and interview improvements to make it accurate for Selenium 4 and modern Java.
1. Explicit Wait (Update for Selenium 4) ✅
Your code is correct.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("result")));
Avoid the old constructor:
new WebDriverWait(driver,10); // Selenium 3
2. Implicit Wait (Selenium 4)
Use:
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
instead of
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
because TimeUnit is deprecated for Selenium 4.
3. Hidden Elements
Your explanation is correct.
A better interview answer:
Selenium cannot directly interact with hidden elements because it simulates real user actions. Hidden elements are usually styled with properties like
display:none,visibility:hidden, or are outside the visible viewport. We can use JavaScript to make the element visible before interacting with it.
4. File Upload
Mention this important interview point.
sendKeys() works only when the application uses
<input type="file">
If the application opens a native Windows file chooser, Selenium cannot control it.
Then use
- AutoIt
- Robot Class
- Sikuli (rarely)
5. Broken Links
Your explanation is correct.
Mention this optimization:
Use
connection.setRequestMethod("HEAD");
instead of
GET
because HEAD downloads only the response headers, making execution much faster.
6. Dynamic Table XPath
Instead of hardcoding
//table//tr[2]//td[3]
mention that row and column numbers can be parameterized.
Example:
int row = 2;
int col = 3;
driver.findElement(
By.xpath("//table//tr[" + row + "]//td[" + col + "]"));
This is commonly asked in interviews.
7. Ajax Controls
Mention why Explicit Wait is preferred.
Interview Answer:
Ajax loads content asynchronously. Explicit Wait waits only for the required element or condition, making tests faster and more reliable than Implicit Wait.
8. Reading Hidden Text
Correct.
Alternative:
String text = (String) js.executeScript(
"return arguments[0].innerText;",
element);
or
textContent
Both are acceptable depending on the HTML.
9. AutoIt
Mention that AutoIt works only on Windows.
For cross-platform automation, Robot Class is more portable.
10. Dynamic Tables
Many interviewers ask how to click a checkbox based on row text.
Example:
driver.findElement(
By.xpath("//td[text()='John']/following-sibling::td/input"))
.click();
This is a practical real-world XPath.
11. Interview Tip
Difference between findElement() and findElements():
| findElement() | findElements() |
|---|---|
| Returns the first matching element | Returns all matching elements |
Throws NoSuchElementException if not found |
Returns an empty list if not found |