How to handle dropdown in selenium? How to handle Iframe elements?

Introduction:

In this article I will be helping students With two Important Questions asked in the interviews. How to handle dropdown and Iframe in selenium for QA Automation engineer Position. So please prepare this question if you are also going for the interview, here is answer of these two Important questions.

How to handle dropdown in selenium?

To Handle Dropdown / listbox in selenium we have two different methods like using SELECT class and by using ACTIONS class lets check when we have to use Select class and when we need to use Actions class:

Note: When we inspect the dropdown webelement and we found that tagname is select then we can use Select class to handle dropdown but when tagname of the dropdown webelement is not select then we can use Actions class.

Select Class:
1. First we need to find the webelement of dropdown.
2. Then Create the object of select class and pass argument as dropdown webelement.
3. by using object of select class we can use methods like selectByVisibleText(), selectByIndex(), selectByValue().

  1. WebElement dropdown = driver.findElement(By.xpath(“//select[@class=’select’]”));
  2. Select s = new Select(dropdown);
  3. s.selectByVisibleText(“12”); // if we have to select date 12
    s.selectByIndex(“11”);

To check Dropdown multiselect:

To check whether a dropdown is multiselect able then use method –> s.isMultiple();
if we get true –> then dropdown is multi selectable.
if we get False –> then dropdown is not multi selectable.
to get all selected options we have method: s.getAllSelectedOptions()
to get first selected options we have method: s.getFirstselectedOption()
we have methods to deselect options from multi select dropdown: s.deselectByIndex(), s.deselectByVisibleText(), s.deselectAll().

To get all options present in dropdown:

to get all options present in dropdown we need to use method –> s.getOptions() or s.getWrappedElements()
List<WebElement> allOptions = s.getOptions();
for(int i=0;i<allOptions.size();i++)
{
System.out.println(allOptions.get(i).gettext());
}

How to handle Iframe elements?

Iframes are created by using tagname Iframe, it is a part of webpage but when we have to inspect the elements in Iframe we need to switch selenium focus from main page to Iframe.
driver.switchTo().frame(parameter);
here we need to pass parameter as ID, Name, Index, WebElement

driver.switchTo().defaultContent() –> this method used to switch selenium focus from iframe to main page
driver.switchTo().parentFrame() –> this method is used to switch selenium focus from iframe to immediate parent frame
so this is how we handle Iframe in selenium or any other automation tool.

QA Automation engineer important interview questions

Conclusion:

I have explained in detail how we can handle dropdown in selenium in both the ways also i have explained how we can handle Iframe in selenium, I hope this answer will help you in interviews.