OS: Win 10.0.17134
Selenium Version: 3.13.0
Browser: Microsoft Edge 42.17134.1.0
Microsoft EdgeHtml 17.17134
Browser Version: Microsoft Edge 42.17134.1.0
Microsoft EdgeHtml 17.17134
Kindly run this script with selenium environment.
import java.awt.AWTException;
import java.util.Arrays;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.Select;
public class EdgeMultipleSelectFail {
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.edge.driver", "drivers/MicrosoftWebDriver.exe");
WebDriver driver = new EdgeDriver();
driver.get("https://html.com/attributes/select-multiple/");
WebElement element = driver.findElement(By.xpath("//*[@id='post-291']/div/div[2]/select"));
Select selectData = new Select(element);
try {
String value = "American flamingo;Andean flamingo;Chilean flamingo;Greater flamingo;James's flamingo;Lesser flamingo";
String[] values = value.split(";");
System.out.println("********" + Arrays.toString(values));
List<WebElement> option = element.findElements(By.tagName("option"));
for (int i = 0; i < values.length; i++) {
boolean match = false;
try {
for (WebElement webElement : option) {
if (webElement.getText().equalsIgnoreCase(values[i])) {
match = true;
selectData.selectByVisibleText(values[i]);
break;
}
}
if (!match) {
System.out.println("No Match Found");
}
} catch (Exception e) {
System.out.println("No Match Found 2");
}
}
} finally {
System.out.println("@@@SID: Inside finally");
}
Thread.sleep(5000);
System.out.println("Let's fetch the selected option again");
List<WebElement> selectedOption = selectData.getAllSelectedOptions();
for (WebElement ele : selectedOption) {
System.out.println("After Selection: " + ele.getText());
}
}
}
I am trying to select all option of select tag using selenium.
It is working fine at chrome and IE. It is only happening in the case of Edge browser.
I tried to test it on other websites and getting the same response.
I was able to reproduce this bug. Here's a simpler SSCCE that won't attempt to cover the screen with a dialog:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import java.util.List;
public class Main {
public static void main(String[] args) {
String driverPath = "C:\\dev\\tools\\selenium\\";
System.setProperty("webdriver.edge.driver", driverPath + "edgedriver.exe");
WebDriver driver = new EdgeDriver();
// System.setProperty("webdriver.firefox.driver", driverPath + "geckodriver.exe");
// WebDriver driver = new FirefoxDriver();
// System.setProperty("webdriver.explorer.driver", driverPath + "IEDriverServer.exe");
// WebDriver driver = new InternetExplorerDriver();
// System.setProperty("webdriver.chrome.driver", driverPath + "chromedriver.exe");
// WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/TAGs/tryit.asp?filename=tryhtml_select_multiple");
driver.switchTo().frame(driver.findElement(By.id("iframeResult")));
WebElement element = driver.findElement(By.name("cars"));
Select selectData = new Select(element);
selectData.selectByIndex(0);
selectData.selectByIndex(1);
selectData.selectByIndex(2);
selectData.selectByIndex(3);
List<WebElement> selectedOptions = selectData.getAllSelectedOptions();
if(selectedOptions.size() != 4) {
System.out.println("ERROR. Expected 4 options to be selected but there was only " + selectedOptions.size() + ".");
}
else{
System.out.println("SUCCESS. There were 4 selected options as expected.");
}
driver.quit();
}
}
Edge is definitely behaving differently than Chrome, FF, and IE. I have no idea what's causing the bug though. #4490 may be related.
In this Microsoft Edge issue, an Edge dev says the following:
The command used to select an element in your example, oSelect.SelectByIndex(i) executes a normal WebDriver click command. It doesn鈥檛 send anything special to the webdriver server to denote that the user would either be holding control or shift, which is what a user would have to do to select multiple options normally. It should however work if you create an Actions object and perform sendKeys off of that object. This way allows for modifier keys to be sent.
This agrees with what I've been able to discover in the selenium Java source.
private void setSelected(WebElement option, boolean select) {
boolean isSelected=option.isSelected();
if ((!isSelected && select) || (isSelected && !select)) {
option.click();
}
}
So selecting a new option simply clicks it. Edge's behavior looks to be correct. What's confusing to me at this point is why all the other browsers don't behave the same as Edge. E.g., clicking a second element in Chrome will always deselect the previous one unless the ctrl or shift keys are held down, which AFAIK they are not.
@GitSage This is because Edge is not WebDriver compliant for clicking on options elements, whereas the other browser are.
https://w3c.github.io/webdriver/#element-click - Section 14.1 Step 7.6.3 states that a click on option should just toggle the state of that option if the container has the "multiple" attribute, not reset the others.
Thank you all for the responses. It helped me to make a genuine report that MS Edge is currently not able to multiple select with the simple select call. I tried Robot class to send CTRL key but It also does not work in case of edge. Finally Action class is working to send CTRL key. Now the below program is working.
package com.SeleniumScript;
import java.awt.AWTException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class EdgeMultiSelectFail2 {
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.edge.driver", "drivers/MicrosoftWebDriver.exe");
WebDriver driver = new EdgeDriver();
driver.get("https://www.w3schools.com/TAGs/tryit.asp?filename=tryhtml_select_multiple");
driver.switchTo().frame(driver.findElement(By.id("iframeResult")));
WebElement element = driver.findElement(By.name("cars"));
Select selectData = new Select(element);
//new Robot().keyPress(KeyEvent.VK_CONTROL);
Actions builder = new Actions(driver);
builder.keyDown(Keys.CONTROL);
builder.perform();
selectData.selectByIndex(0);
selectData.selectByIndex(1);
selectData.selectByIndex(2);
selectData.selectByIndex(3);
//new Robot().keyRelease(KeyEvent.VK_CONTROL);
builder.keyUp(Keys.CONTROL);
builder.perform();
List<WebElement> selectedOptions = selectData.getAllSelectedOptions();
if(selectedOptions.size() != 4) {
System.out.println("ERROR. Expected 4 options to be selected but there was only " + selectedOptions.size() + ".");
}
else{
System.out.println("SUCCESS. There were 4 selected options as expected.");
}
Thread.sleep(2000);
driver.quit();
}
}
Closing this issue as it should be fixed in Edge driver by Microsoft.
I too facing the same issue not able to select more than one value from list of values can please help us on this. and let me know the actual issue. Am wondering for that more than day it is priority to me.
package iframehandler;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class EdgeMultiSelectFail {
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.chrome.driver", "D:\\\\Eclipse\\\\Drivers\\\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://techcanvass.com/Examples/multi-select.html");
//driver.switchTo().frame(driver.findElement(By.id("iframeResult")));
WebElement element = driver.findElement(By.id("multiselect"));
Select selectData = new Select(element);
new Robot().keyPress(KeyEvent.VK_CONTROL);
Actions builder = new Actions(driver);
// builder.keyDown(Keys.LEFT_SHIFT).click();
// builder.perform();
selectData.selectByIndex(2);
builder.keyDown(Keys.CONTROL).sendKeys("a");
selectData.selectByIndex(1);
builder.keyDown(Keys.LEFT_SHIFT).click();
selectData.selectByIndex(2);
selectData.selectByIndex(3);
//new Robot().keyRelease(KeyEvent.VK_CONTROL);
builder.keyUp(Keys.CONTROL);
builder.perform();
List<WebElement> selectedOptions = selectData.getAllSelectedOptions();
if(selectedOptions.size() != 4) {
System.out.println("ERROR. Expected 4 options to be selected but there was only " + selectedOptions.size() + ".");
}
else{
System.out.println("SUCCESS. There were 4 selected options as expected.");
}
Thread.sleep(2000);
driver.quit();
}
}
@sivakk2003 what else do you want to know that's not already in the comments? We've explained why it happens, exactly how Edge isn't compliant with the spec, exactly why the Edge team needs to be the ones to fix this, and the OP posted a functioning code sample of a workaround that handles the situation on Edge.
What's your question exactly?
How can this be reported to Microsoft?
@ffMathy There used to be regular mechanisms for this. However, given that Microsoft has announced that they're migrating Edge to be based on Chromium, and that they've redirected all development efforts toward that effort, which implies that no development attention is being given to fixing issues in the existing implementation, I'm not sure what good reporting it would do.
It appears that he already posted to the Edge developer issue tracker https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/20638074/
Most helpful comment
@GitSage This is because Edge is not WebDriver compliant for clicking on options elements, whereas the other browser are.
https://w3c.github.io/webdriver/#element-click - Section 14.1 Step 7.6.3 states that a click on option should just toggle the state of that option if the container has the "multiple" attribute, not reset the others.