This is my code and URL. This code is working fine my doubt is why we are not using .click();
in below code:
Select s1 = new Select(driver.findElement(By.id("month")));
This is my code and URL. This code is working fine my doubt is why we are not using .click();
in below code:
Select s1 = new Select(driver.findElement(By.id("month")));
The Select
class just provides a convenient abstraction layer over the select
and option
elements. Internally, though, the selectBy*
methods still do the proper clicking (source code reference):
private void setSelected(WebElement option, boolean select) {
boolean isSelected=option.isSelected();
if ((!isSelected && select) || (isSelected && !select)) {
option.click(); // < HERE!
}
}
It is just encapsulated inside the Select
class implementation.
That's because the Select class has this constructor:
Select(WebElement element)
Moreover logically you won't be selecting Select tag
..you would be selecting options
...so there are various methods defined to select options in different ways..see Reference Docs
All methods defined here to select options, include this method internally...
private void setSelected(WebElement option, boolean select) {
boolean isSelected=option.isSelected();
if ((!isSelected && select) || (isSelected && !select)) {
option.click();
}
}
So if you call one of those methods...e.g.selectByIndex(int index)
they will include click internally.
In the automation testing company elements that have 'SELECT TAG' in it to select these elements we use Select select = new Select(WebElement element). Then we perform the desired action using select functions select.selectByVisibleText(string text).
As in https://www.facebook.com/ we have select tag for Day, Month and Year for Birthday section we use below code for selecting month from the drop down.(Say Jan)
Select s1 = new Select(driver.findElement(By.id("month")));
s1.select.selectByVisibleText("Jan");
Note: In select functions selectByVisibleText(), selectByValue() and selectByIndex(),We do not use click(), As select functions already have click() inside the wrapper methods.
Pre defined Select function example that already contains click().
public void selectByIndex(int index) {
String match = String.valueOf(index);
boolean matched = false;
for (WebElement option : getOptions()) {
if (match.equals(option.getAttribute("index"))) {
setSelected(option);
if (!isMultiple()) {
return;
}
matched = true;
}
}
if (!matched) {
throw new NoSuchElementException("Cannot locate option with index: " + index);
}
}
private void setSelected(WebElement option) {
if (!option.isSelected()) {
option.click();
}
}
Hoping this may clear the doubt
Thanks