SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.5k stars 8.15k forks source link

[🐛 Bug]: SelectElement not working after updating selenium webdriver 4 and IEDriverServer 4 #10319

Closed shlokVK closed 2 years ago

shlokVK commented 2 years ago

What happened?

after updating selenium WebDriver (3.15 to 4.0.0) and IEDriver (3.150.1 to 4.0.0) getting below error while setting dropdown value, code was working properly with old selenium version (3): Error : Cannot click on option element. Executing JavaScript click function returned an unexpected error, but no error could be returned from Internet Explorer's JavaScript engine

HTML :

C# code : public By countrySelector = By.Id("selCountry"); public string Country { set => new SelectElement(Driver.FindElement(countrySelector )).SelectByText(value); }

   Country= "India";

How can we reproduce the issue?

HTML : 
<TD vAlign=top><SELECT id=selCountry">
<OPTION selected value=0>None</OPTION> <OPTION value=1>India</OPTION></SELECT>
</TD>

 public By countrySelector = By.Id("selCountry");

        public string Country
        {
            set => new SelectElement(Driver.FindElement(countrySelector )).SelectByText(value);
        }

Country= "India";

Relevant log output

Cannot click on option element. Executing JavaScript click function returned an unexpected error, but no error could be returned from Internet Explorer's JavaScript engine

Operating System

Window server 2016

Selenium version

C# Selenium version 4 , IEDriver 4

What are the browser(s) and version(s) where you see this issue?

Internet Explorer 11

What are the browser driver(s) and version(s) where you see this issue?

IEDriver 4

Are you using Selenium Grid?

no

github-actions[bot] commented 2 years ago

@shlokVK, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

titusfortner commented 2 years ago

Every other example of this exception I've seen has been when working with drop-downs that aren't actually select/option elements. This functionality shouldn't have changed between Selenium 3 & 4. Can you copy/paste the complete HTML for the form you are working with?

shlokVK commented 2 years ago

Every other example of this exception I've seen has been when working with drop-downs that aren't actually select/option elements. This functionality shouldn't have changed between Selenium 3 & 4. Can you copy/paste the complete HTML for the form you are working with?

thanks for reply HTML Sample Code :

CountryPage                                                                                                                                                            

Code is working properly with below Scenario:

Scenario 1

Selenium.Webdriver : v3.141.0 Selenium.Support : v3.141.0 Selenium.WebDriver.IEDriver : v3.150.1.2

Scenario 2

Selenium.Webdriver : v4.1.0 Selenium.Support : v4.1.0 Selenium.WebDriver.IEDriver : v3.150.1.2

Code is not working properly with below Scenario:

Scenario 1

Selenium.Webdriver : v4.1.0 Selenium.Support : v4.1.0 Selenium.WebDriver.IEDriver : v4.0.0 Sample HTML.txt

nibinki commented 2 years ago

I am facing same issue in selenium 4.1 , ie driver 4 and working in Internet explorer mode using edge . Same code was working on previous versions . None of the select elements working eventhough we can view the options but not able to select any value . If there is any alternative for this please suggest.

bluesbox1 commented 2 years ago

This is the exact issue I encountered yesterday after updating to Selenium 4 and IEDriverServer 4.0.0 in order to test our website in Edge compatibility mode. Following this issue. Hope fix will be deployed soon.

mnstylianou commented 2 years ago

I have the same issue after updating to Selenium 4 and IEDriverServer 4.0.0 (and java) in order to test a bank application in Edge compatibility mode

mnstylianou commented 2 years ago

This is a blocker for me as you must select a role and a branch to log in to the application, so I have even tried to do a workaround using Actions:

default T actionSelect(By by, String text){
        Actions actions = new Actions(getDriver());
        getDriver().manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
        WebElement dropElement=getDriver().findElement(by);     //find the dropdown
        dropElement.click();
        Select dropOptions = new Select(dropElement);
        int elements = dropOptions.getOptions().size();    //number of elements in dropdown

        for (int i = 0; i < elements; i++) {    //for each element
            actions.sendKeys(Keys.DOWN,Keys.DOWN,Keys.DOWN).build().perform();   //arrow down
            Log.message("value is = " + i + ": "+dropOptions.getOptions().get(i).getText());

            if (dropOptions.getOptions().get(i).getText().equals(text))   //check current element
            {
                actions.sendKeys(Keys.ENTER).build().perform();  //Send Keys.Enter when found
                break;
            }
        }
        return (T) this;
    }

It fails with the same error: org.openqa.selenium.JavascriptException: Cannot click on option element. Executing JavaScript click function returned an unexpected error, but no error could be returned from Internet Explorer's JavaScript engine.

mnstylianou commented 2 years ago

The following modified code using actions sends a TAB when the correct entry is found and it works:

default T actionSelect(By by, String text) {
        Actions actions = new Actions(getDriver());
        getDriver().manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
        WebElement ele = getDriver().findElement(by);
        Select dropOptions = new Select(ele);
        ele.click();
        int elements = dropOptions.getOptions().size();
        for (int i = 0; i < elements; i++) {
            if (dropOptions.getOptions().get(i).getText().equals(text)) {
                Log.message("Found value: " + dropOptions.getOptions().get(i).getText());
                actions.sendKeys(Keys.TAB).build().perform();
                break;
            } else {
                Log.message("value is = " + i + ": " + dropOptions.getOptions().get(i).getText());
                actions.sendKeys(Keys.DOWN).build().perform();
            }
        }
        return (T) this;
    }
kameshgvm commented 2 years ago

I am also facing the same issue. Above solution is not that much feasible when there are many values in the dropdown and time consuming.

mnstylianou commented 2 years ago

I am also facing the same issue. Above solution is not that much feasible when there are many values in the dropdown and time consuming.

Very true. It is slow and clumsy but at least I'm not blocked any more. It is only meant to be a temporary solution till this issue is fixed properly

AradhanaJamwal commented 2 years ago

Any resolution on this issue..facing same challenge.. Seems like IE driver 4 or later binding has this issue...

shlokVK commented 2 years ago

Any resolution on this issue..facing same challenge.. Seems like IE driver 4 or later binding has this issue...

no there is no any resolution yet.

ZooDoo4U commented 2 years ago

Might assert this is not limited to the IE Driver, the error i'm getting the exception with Chrome. I run with Chrome and Firefox and it seems Firefox is fine. That said comparing these two the Chrome version executes super fast, the Firefox is really slow... Will look into the Support UI source for the SelectElement if that source is available.

The execution time is interesting. Chrome takes about 50ms while Firefox 1 second. Just seems there is some additional checks/waits that is allowing Firefox to work...

My Vitals: Win 10 VS 2019 C# .Net 4.8 Chrome Driver: 100.0.4896.6000 Firefox Driver: 0.30.0.1 Selenium: 4.1.0

I believe these are the latest/greatest/current versions for all...

Hmm... Going through and double checking stuff, i'm seeing the nuget project reference is 4.1.0, But in the dll in the lib folder is 4.0.0? Normal or mismatched???
Specifically: .\Selenium.Support.4.1.0\lib\net48\WebDriver.Support.dll

Dll date == 11/22/2021 -- matches reported date from Nuget page publishdate, so a typo in the DLL's version info...

diemol commented 2 years ago

For the ones who are facing this issue and using IE. Have you tried to use Edge in the IE Mode? What are the results? Please share a GitHub repository we can clone to reproduce the issue.

madrian13 commented 2 years ago

@diemol I tried and got same issue with Edge in IE Mode.

ZooDoo4U commented 2 years ago

Interesting one thing investigating my code, seems i might be running into an error by pushing my website too fast. Seems my code was incorrectly reading the number of options within the

madrian13 commented 2 years ago

@diemol I think I've managed to reproduce it with a simple page I created the issue got reproduced only after adding in the page the line below to enforce IE7 mode

After that I got the error below ... Message: Test method SelectIssueIEMode.Tests.TestSelect threw exception: OpenQA.Selenium.JavaScriptException: Cannot click on option element. Executing JavaScript click function returned an unexpected error, but no error could be returned from Internet Explorer's JavaScript engine.

madrian13 commented 2 years ago

@diemol I just uploaded the project here: https://github.com/madrian13/SelectIssueIEMode

shlokVK commented 2 years ago

In my sample test application when I hit standard http://www.tizag.com/phpT/examples/formex.php" website URL, select drop down works properly.

When I Hit my application url in same sample application code. Select dropdown does not work properly. I am getting same javascript error.

Selenium Webdriver version : 4.0.0 IEDriver version : 4.0.0

and same selenium code works properly when I changed IEDriver version 4.0.0 to 3.150.1.2

madrian13 commented 2 years ago

@diemol does the example I provided work for you ? Can you reproduce the issue ? Any hope to get this issue fixed ?

kite39 commented 2 years ago

I found a solution! Use JavaScriptExecutor.

Selenium Webdriver version : 4.1.3 IEDriver version : 4.0.0

 public void selectOption(WebDriver wdriver,WebElement wele,String value) throws Exception {
      JavascriptExecutor jexec = (JavascriptExecutor) wdriver;
      jexec.executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", wele, value);
  }

  public void deselectOption(WebDriver wdriver,WebElement wele) throws Exception {
      JavascriptExecutor jexec = (JavascriptExecutor) wdriver;
      jexec.executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ select.options[i].selected = false; } ", wele);
    }

■Samlple exec code
WebElement sitem = windowDriver.findElement(By.xpath("//select[@name='itemSel']"));
selectOption(windowDriver, sitem, "test object");
kameshgvm commented 2 years ago

Thanks Kite. It is working for me. But, I am following few issues after implementing it.

  1. Unable to select value using index. (what changes to do in the code).
  2. It is only selecting values but not rendering. i.e. after selecting values from 1st dropdown, screen should refresh and values in the 2nd dropdown should be populated. This is not happening.
  3. In one of the existing application few buttons are not working with one click. When I am clicking 2nd time it is working. Example Search button is fetching results only on clicking 2nd time. (Weird behavior though your code is implemented for dropdown.)
mnstylianou commented 2 years ago

This is what I've been using to select by index:

default T jsSelectByIdAndIndex(String id, String index) {

        ((JavascriptExecutor) getDriver()).executeScript("return document.getElementById('" + id + "').selectedIndex = '" + index + "'");

        return (T) this;
    }
mnstylianou commented 2 years ago

The above uses the elements @id. To use the element's xpath:

 default T jsSelectByXpathAndOption(String xpath, String option, Integer index) {
        getDriver().manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
        By by = By.xpath(xpath);
        if (index != null) {
            ((JavascriptExecutor) getDriver()).executeScript("return document.evaluate(\"" + xpath + "\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.selectedIndex = '" + index + "'");
        } else {
            WebElement ele = getDriver().findElement(by);
            Select dropOptions = new Select(ele);
            int elements = dropOptions.getOptions().size();
            for (int i = 0; i < elements; i++) {
                if (dropOptions.getOptions().get(i).getText().equals(option)) {
                    Log.message("Found value: " + dropOptions.getOptions().get(i).getText());
                    ((JavascriptExecutor) getDriver()).executeScript("return document.evaluate(\"" + xpath + "\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.selectedIndex = '" + i + "'");
                    break;
                }
            }
        }
        return (T) this;
    }
RNDaniel commented 2 years ago

I found a solution! Use JavaScriptExecutor.

Selenium Webdriver version : 4.1.3 IEDriver version : 4.0.0

 public void selectOption(WebDriver wdriver,WebElement wele,String value) throws Exception {
      JavascriptExecutor jexec = (JavascriptExecutor) wdriver;
      jexec.executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", wele, value);
  }

  public void deselectOption(WebDriver wdriver,WebElement wele) throws Exception {
      JavascriptExecutor jexec = (JavascriptExecutor) wdriver;
      jexec.executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ select.options[i].selected = false; } ", wele);
  }

■Samlple exec code
WebElement sitem = windowDriver.findElement(By.xpath("//select[@name='itemSel']"));
selectOption(windowDriver, sitem, "test object");

Thank you very much you are an angel. I'm working in a bank too with a very old system and I couldn't do my job because of this bug. I wrote it in python and this finally works for me.

diemol commented 2 years ago

Thank you all for finding the root cause of the issue (IE7 mode) and finding ways to provide a workaround. The project currently only supports IE11, and soon only the IE Mode in Edge.

I will close this issue but feel free to continue the conversation in the comments.

shlokVK commented 2 years ago

Thank you all for finding the root cause of the issue (IE7 mode) and finding ways to provide a workaround. The project currently only supports IE11, and soon only the IE Mode in Edge.

I will close this issue but feel free to continue the conversation in the comments.

Javascript executor is the work around for this issue. however, what about original SelectElement class issue ? because it is a best practice to use Selenium SelectElement methods to set/select drop down option values

diemol commented 2 years ago

You mentioned that it works except when you use IE7 compatibility mode on the site, which is the reason for the bug, since currently only IE 11 is supported.

To my understanding, from your research, you can use SelectElement when the site does not force IE 7 mode. So, based on what we currently support, it is fine, right?

madrian13 commented 2 years ago

@diemol Not sure I understood it right, who is not suporting IE7/5 compatibility mode, IEDriver 4 ? Is there an oficial documentation where it's written wich are the supported versions (Sorry but I am not sure if you refer to the one I found below). Is there any plan to add support for the older versions ? (since they still exist in Edge in IE Mode )

_https://www.selenium.dev/documentation/ie_driver_server/ The InternetExplorerDriver is a standalone server which implements WebDriver’s wire protocol. This driver has been tested with IE 11, and on Windows 10. It might work with older versions of IE and Windows, but this is not supported._

ZooDoo4U commented 2 years ago

@diemol, @madrian13, Been there when working for companies and needing to support stuff... But IE7? It came out in 2006, and if memory serves me, people moved off it to IE8 because IE7 had issues, more because everyone was trying to maintain and support compatibility with IE6. Not only is the browser itself flaky and problematic, the older version of IE were deprecated because of so many security vulnerabilities. Really do need to get with the times...?

madrian13 commented 2 years ago

@ZooDoo4U Totally agree. But I'm not the one that needs to be convinced. I'm your side here. Yet , even if it's hard to believe, there are some companies that for some reasons still use some old web apps that only run with that compat mode enabled ...

ZooDoo4U commented 2 years ago

@madrian13, Might suggest for those needing to support stuff that old, might do better to "go with the flow", meaning use the older version of Selenium that matched up with IE7 as well as other tools like VS 2008... Seems insane to mix and match such wildly different versions, too much changes, thing are deprecated in the OS... Can't complain when things go south, while the weather down there might be nicer ;)

madrian13 commented 2 years ago

@ZooDoo4U Problem is that IE 11 is going to be removed from windows and older IeDriver (v3.x) had some issues with Edge(IE Mode) that were supposed to be fixed in the new version (v4.x). Also , according to https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/ie-mode?tabs=c-sharp To begin automating tests in IE mode in Microsoft Edge, download IEDriver. Make sure that the version of IEDriver that you download is 4.0.0.0 or greater.

ZooDoo4U commented 2 years ago

@madrian13, Not saying you would disagree, my problem with the quoted link... "automating tests in IE mode in Microsoft Edge". My first response would be, so test in Edge and be done with it. Understood many have old plug-ins, OCX controls, are the main driving factor, but IE 7 is 15 or more years old and easily 5 years past end of life/support... Then again if there is a bug in the old plug in... Is the bug Selenium's issue or who's?

shlokVK commented 2 years ago

You mentioned that it works except when you use IE7 compatibility mode on the site, which is the reason for the bug, since currently only IE 11 is supported.

To my understanding, from your research, you can use SelectElement when the site does not force IE 7 mode. So, based on what we currently support, it is fine, right?

@diemol I didn't mention anything about IE7 Compatibility. SelectElement is working properly with IE11 and IEDriver version 3.150.1.2. also same code works properly with Edge in the IE Mode. so there is no any compatibility issue. but same SelectElement code is not working properly after updating IEDriver version 3.150 to 4.

diemol commented 2 years ago

You are right, what I meant is to repeat what was said above. If the site requests this https://github.com/madrian13/SelectIssueIEMode/blob/master/SelectIssueIEMode/login.html#L4, then it does not work, which is out of the scope if the currently supported versions.

shlokVK commented 2 years ago

You are right, what I meant is to repeat what was said above. If the site requests this https://github.com/madrian13/SelectIssueIEMode/blob/master/SelectIssueIEMode/login.html#L4, then it does not work, which is out of the scope if the currently supported versions.

@diemol got it thanks for the clarification. is there any chances to resolve original issue (SelectElement) in next IEDriver release for IE 11 or Edge in the IE Mode?

diemol commented 2 years ago

I do not think so, supporting versions that are already EOL does not make sense. You can ask to the Edge folks if they would consider it https://github.com/MicrosoftEdge/EdgeWebDriver, but I cannot imagine that.

shlokVK commented 2 years ago

We have to use InternetExplorerDriver to execute test in the Edge browser with IE mode. because our application works only in Internet Explorer browser (it's old web application). I don't think this issue is related with EDge Webdriver. this is something related with IEDriver.

`InternetExplorerOptions optIE = new InternetExplorerOptions(); optIE.AttachToEdgeChrome = true; optIE.EdgeExecutablePath = edgeExecutablePath;

var webdriver = new InternetExplorerDriver(InternetExplorerDriverService.CreateDefaultService(), optIE); `

diemol commented 2 years ago

I point to the Edge team because they are now kindly helping us with the IEDriver. But again, I doubt we want to add backwards compatibility for a version that is EOL since many years ago.

shlokVK commented 2 years ago

I point to the Edge team because they are now kindly helping us with the IEDriver. But again, I doubt we want to add backwards compatibility for a version that is EOL since many years ago.

@diemol okay. thank you for your input.

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.