๐งฐ Handling Dropdowns and Multiple Windows in Selenium
In many test scenarios, you’ll need to:
Select items from dropdown menus (also called select boxes).
Switch between multiple browser windows or tabs.
Selenium provides built-in support for both tasks.
✅ 1. Handling Dropdowns
Selenium handles HTML <select> dropdowns using the Select class.
๐ Example in Java
java
Copy
Edit
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class DropdownExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement dropdown = driver.findElement(By.id("dropdownId"));
Select select = new Select(dropdown);
// Select by visible text
select.selectByVisibleText("Option 1");
// Select by value
select.selectByValue("value1");
// Select by index
select.selectByIndex(2);
driver.quit();
}
}
๐ Example in Python
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
driver.get("https://example.com")
dropdown = driver.find_element("id", "dropdownId")
select = Select(dropdown)
select.select_by_visible_text("Option 1")
select.select_by_value("value1")
select.select_by_index(2)
driver.quit()
๐ Note: For custom dropdowns (non-HTML <select>), you'll interact with the elements directly using .click() and .find_element() logic.
✅ 2. Handling Multiple Windows or Tabs
Steps:
Store the original window handle.
Trigger the action that opens a new window/tab.
Get all window handles.
Switch to the new one.
Interact with it.
Switch back if needed.
๐ Java Example
java
Copy
Edit
String originalWindow = driver.getWindowHandle();
driver.findElement(By.linkText("Open New Tab")).click();
for (String windowHandle : driver.getWindowHandles()) {
if (!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
break;
}
}
// Do actions in new window
System.out.println(driver.getTitle());
// Switch back
driver.switchTo().window(originalWindow);
๐ Python Example
python
Copy
Edit
original = driver.current_window_handle
driver.find_element("link text", "Open New Tab").click()
for handle in driver.window_handles:
if handle != original:
driver.switch_to.window(handle)
break
print(driver.title) # Actions in new window
driver.switch_to.window(original) # Switch back
๐ Summary
Task Method (Java / Python)
Select dropdown new Select(element) / Select(element)
Select by text .selectByVisibleText() / .select_by_visible_text()
Get current window driver.getWindowHandle() / driver.current_window_handle
Get all windows driver.getWindowHandles() / driver.window_handles
Switch window driver.switchTo().window(handle) / driver.switch_to.window(handle)
Learn Selenium JAVA Training in Hyderabad
Read More
Using TestNG with Selenium: Why and How
Top 10 Selenium WebDriver Commands Every Beginner Should Know
Understanding XPath and CSS Selectors in Selenium
Visit Our Quality Thought Training in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments