21. How can we select elements by their attribute value using CSS Selector?
Ans:
We can use [attribute=value] syntax to select elements by attribute.
Example:
css
[type=radio]
This will select all elements where type=”radio”.
Q22. What is the fundamental difference between XPath and CSS Selector?
Ans:
- XPath can traverse both up and down the DOM (Document Object Model).
- CSS Selector can only traverse downwards in the DOM.
Q23. How can we launch different browsers in Selenium WebDriver?
Ans:
By creating an instance of the browser-specific WebDriver class:
java
WebDriver driver = new ChromeDriver(); // For Chrome
WebDriver driver = new FirefoxDriver(); // For Firefox
Q24. What is the use of driver.get(“URL”) and driver.navigate().to(“URL”)?
Ans:
Both are used to open a URL. Functionally, there is no significant difference.
Example:
java
driver.get(“https://example.com”);
driver.navigate().to(“https://example.com”);
Q25. How can we type text in a textbox element using Selenium?
Ans:
java
WebElement searchBox = driver.findElement(By.id(“search”));
searchBox.sendKeys(“Selenium”);
Q26. How can we clear text written in a textbox?
Ans:
Use the clear() method:
java
driver.findElement(By.id(“elementLocator”)).clear();
Q27. How to check a checkbox in Selenium?
Ans:
Use the click() method:
java
driver.findElement(By.id(“checkboxId”)).click();
Q28. How can we submit a form in Selenium?
Ans:
You can use either:
java
driver.findElement(By.id(“formId”)).submit();
or
java
driver.findElement(By.id(“submitButton”)).click();
Q29. What is the difference between close() and quit() commands?
Ans:
- close() closes the current browser window.
- quit() closes all browser windows opened by WebDriver.
Q30. How to switch between multiple windows in Selenium?
Ans:
java
for(String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
Q31. What is the difference between getWindowHandle() and getWindowHandles()?
Ans:
- getWindowHandle() returns the handle (ID) of the current window.
- getWindowHandles() returns a set of all open window handles.
Q32. How can we move to a particular frame in Selenium?
Ans:
java
driver.switchTo().frame(0); // By index
driver.switchTo().frame(“frameName”); // By name
driver.switchTo().frame(“frameId”); // By ID
Q33. Can we move back and forward in a browser using Selenium?
Ans:
Yes, using:
java
driver.navigate().back();
driver.navigate().forward();
Q34. Is there a way to refresh the browser using Selenium?
Ans:
Yes, you can refresh the page using:
java
driver.navigate().refresh();
Or:
java
driver.findElement(By.tagName(“body”)).sendKeys(Keys.F5);
Q35. How can we maximize the browser window in Selenium?
Ans:
java
driver.manage().window().maximize();
Q36. How can we fetch the text written over an element?
Ans:
java
String text = driver.findElement(By.id(“elementLocator”)).getText();
Q37. How can we find the value of different attributes like name, class, or value of an element?
Ans:
java
String attrValue = driver.findElement(By.id(“elementLocator”)).getAttribute(“value”);
Q38. How to delete cookies in Selenium?
Ans:
java
driver.manage().deleteAllCookies();
Q39. What is an implicit wait in Selenium?
Ans:
It waits for a set amount of time for elements to appear before throwing an exception.
java
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Q40. What is an explicit wait in Selenium?
Ans:
It waits until a specific condition is met for a particular element.
java
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(“elementId”)));
ChatGPT said:
You’re giving feedback on a new version of ChatGPT.
Which response do you prefer? Responses may take a moment to load.
ChatGPT
Response 1
Updated saved memory
Here’s the beginner-friendly version of Selenium Interview Questions 41–60 with simplified answers: