Selenium DeselectAll not working for HTML SELECT with multiple attribute











up vote
1
down vote

favorite












I am using this HTML code:



<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>


Manually I select some items. Then I want to deselect all of them (I am using C# but that does not matter):



var carsElement = BrowserDriver.FindElementByName("cars");
var carsSelect = new SelectElement(carsElement);
carsSelect.DeselectAll();


What happens: The first selected options stays selected, others are unselected.



Looking at the code this is what must happen, because DeselectAll() calls Click() for all selected options. You can try that in your browser. This will never unselect all options (unless you hold CTRL while clicking but that is not done by the Selenium code). So the correct way would be to change DeselectAll to press CTRL while clicking as demonstrated by How to perform Control key down in selenium webdriver?



Bottom line, I know how to fix this; my questions are: Am I missing anything? Is there an easier way? Is SelectElement not intended from HTML SELECT multiple?










share|improve this question




























    up vote
    1
    down vote

    favorite












    I am using this HTML code:



    <select name="cars" multiple>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
    </select>


    Manually I select some items. Then I want to deselect all of them (I am using C# but that does not matter):



    var carsElement = BrowserDriver.FindElementByName("cars");
    var carsSelect = new SelectElement(carsElement);
    carsSelect.DeselectAll();


    What happens: The first selected options stays selected, others are unselected.



    Looking at the code this is what must happen, because DeselectAll() calls Click() for all selected options. You can try that in your browser. This will never unselect all options (unless you hold CTRL while clicking but that is not done by the Selenium code). So the correct way would be to change DeselectAll to press CTRL while clicking as demonstrated by How to perform Control key down in selenium webdriver?



    Bottom line, I know how to fix this; my questions are: Am I missing anything? Is there an easier way? Is SelectElement not intended from HTML SELECT multiple?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am using this HTML code:



      <select name="cars" multiple>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
      </select>


      Manually I select some items. Then I want to deselect all of them (I am using C# but that does not matter):



      var carsElement = BrowserDriver.FindElementByName("cars");
      var carsSelect = new SelectElement(carsElement);
      carsSelect.DeselectAll();


      What happens: The first selected options stays selected, others are unselected.



      Looking at the code this is what must happen, because DeselectAll() calls Click() for all selected options. You can try that in your browser. This will never unselect all options (unless you hold CTRL while clicking but that is not done by the Selenium code). So the correct way would be to change DeselectAll to press CTRL while clicking as demonstrated by How to perform Control key down in selenium webdriver?



      Bottom line, I know how to fix this; my questions are: Am I missing anything? Is there an easier way? Is SelectElement not intended from HTML SELECT multiple?










      share|improve this question















      I am using this HTML code:



      <select name="cars" multiple>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
      </select>


      Manually I select some items. Then I want to deselect all of them (I am using C# but that does not matter):



      var carsElement = BrowserDriver.FindElementByName("cars");
      var carsSelect = new SelectElement(carsElement);
      carsSelect.DeselectAll();


      What happens: The first selected options stays selected, others are unselected.



      Looking at the code this is what must happen, because DeselectAll() calls Click() for all selected options. You can try that in your browser. This will never unselect all options (unless you hold CTRL while clicking but that is not done by the Selenium code). So the correct way would be to change DeselectAll to press CTRL while clicking as demonstrated by How to perform Control key down in selenium webdriver?



      Bottom line, I know how to fix this; my questions are: Am I missing anything? Is there an easier way? Is SelectElement not intended from HTML SELECT multiple?







      selenium select selenium-webdriver webdriver multi-select






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 10:13









      DebanjanB

      37.4k73373




      37.4k73373










      asked Nov 22 at 9:38









      Jack Miller

      1,61511832




      1,61511832
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Select class can handle multiple choice dropdown. It even checks if the dropdown is multiple choice when using DeselectAll(). From github



          public void DeselectAll()
          {
          if (!this.IsMultiple)
          {
          throw new InvalidOperationException("You may only deselect all options if multi-select is supported");
          }

          foreach (IWebElement option in this.Options)
          {
          SetSelected(option, false);
          }
          }

          private static void SetSelected(IWebElement option, bool select)
          {
          bool isSelected = option.Selected;
          if ((!isSelected && select) || (isSelected && !select))
          {
          option.Click();
          }
          }


          When you click on the first select option without pressing the control key this option remains selected but all the other options are being deselected, so the click is actually not performed on the rest of the options since both isSelected and select are false in SetSelected.



          The solution is either implement your own DeselectAll() as suggested in your question, or select the first option from the dropdown (which will automatically deselect all the other options) and then deselect this option using control.






          share|improve this answer





















          • So DeselectAll() indeed does not deselect all. But it should according to the method name and the documentation: "Clear all selected entries."
            – Jack Miller
            Nov 22 at 10:22












          • @JackMiller Yes, it should. I used it before without problem but the dropdown was different, the options where visible only after clicking the dropdown and it wasn't necessary to hold ctrl for multiple choice. Maybe that's the problem.
            – Guy
            Nov 22 at 11:24


















          up vote
          2
          down vote













          You can for sure deselect these with



          browser.execute_script("[...document.querySelectorAll('[name=cars] option')].map(o => o.selected = false)")





          share|improve this answer






























            up vote
            0
            down vote













            Even though I did not ask for code how to set the selection state, I'll post it anyways for anybody who encountered the same problem. This is C# code. You'll need NuGet packages Selenium.WebDriver and Selenium.Support. BrowserDriver is a member variable of my helper class which contains this method.



            /// <summary>
            /// Sets the selection state of <paramref name="selectElement"/>. All options specified by <paramref name="selectedOptions"/>
            /// are select, all others are unselected.
            /// </summary>
            /// <param name="selectElement">HTML select element</param>
            /// <param name="selectedOptions">options to be selected</param>
            internal void SelectStateByText(OpenQA.Selenium.Support.UI.SelectElement selectElement, params string selectedOptions)
            {
            Assert.IsNotNull(selectElement);
            Assert.IsNotNull(selectedOptions);
            CollectionAssert.IsSubsetOf(selectedOptions, selectElement.Options.Select(o => o.Text).ToArray());
            if (!selectElement.IsMultiple)
            {
            Assert.AreEqual(1, selectedOptions.Length);
            selectElement.SelectByText(selectedOptions[0]);
            }
            else
            {
            var actions = new OpenQA.Selenium.Interactions.Actions(BrowserDriver);
            actions.KeyDown(Keys.LeftControl);
            foreach (var option in selectElement.Options)
            {
            if (selectedOptions.Contains(option.Text) && !option.Selected)
            {
            actions.Click(option);
            }
            else if (option.Selected)
            {
            actions.Click(option);
            }
            }
            actions.KeyUp(Keys.LeftControl).Build().Perform();
            }
            }





            share|improve this answer




























              up vote
              -1
              down vote













              I have verified your usecase of using the deselect_all() method through the Selenium Python Client and it seems to work perfecto.



              deselect_all()



              deselect_all() method clears all selected entries. This is only valid when the SELECT supports multiple selections. throws NotImplementedError If the SELECT does not support multiple selections.





              Illustration



              Note: As you have mentioned in your question I have also simulated the selection of all the items manually





              • Code Block:



                from selenium import webdriver
                from selenium.webdriver.common.by import By
                from selenium.webdriver.support.ui import WebDriverWait
                from selenium.webdriver.support import expected_conditions as EC
                from selenium.webdriver.support.ui import Select
                import time

                options = webdriver.ChromeOptions()
                options.add_argument("start-maximized")
                options.add_argument('disable-infobars')
                driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
                driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple')
                WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframeResult")))
                select_cars = Select(driver.find_element_by_css_selector("select[name='cars']"))
                time.sleep(5) # Timeframe to Manually select all the items
                select_cars.deselect_all()


              • Browser Snapshot:



              deselect_all.gif






              share|improve this answer























              • Very interesting! In the Python code I see that the same things happens: .Click() of the element(s) is called. Is it possible that you call your Python script using a shortcut which includes CTRL?
                – Jack Miller
                Nov 22 at 11:22










              • @JackMiller Much more interesting is the fact that you find this answer interesting yet this answer is the only downvoted answer among several :)
                – DebanjanB
                Nov 23 at 7:55











              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427859%2fselenium-deselectall-not-working-for-html-select-with-multiple-attribute%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              Select class can handle multiple choice dropdown. It even checks if the dropdown is multiple choice when using DeselectAll(). From github



              public void DeselectAll()
              {
              if (!this.IsMultiple)
              {
              throw new InvalidOperationException("You may only deselect all options if multi-select is supported");
              }

              foreach (IWebElement option in this.Options)
              {
              SetSelected(option, false);
              }
              }

              private static void SetSelected(IWebElement option, bool select)
              {
              bool isSelected = option.Selected;
              if ((!isSelected && select) || (isSelected && !select))
              {
              option.Click();
              }
              }


              When you click on the first select option without pressing the control key this option remains selected but all the other options are being deselected, so the click is actually not performed on the rest of the options since both isSelected and select are false in SetSelected.



              The solution is either implement your own DeselectAll() as suggested in your question, or select the first option from the dropdown (which will automatically deselect all the other options) and then deselect this option using control.






              share|improve this answer





















              • So DeselectAll() indeed does not deselect all. But it should according to the method name and the documentation: "Clear all selected entries."
                – Jack Miller
                Nov 22 at 10:22












              • @JackMiller Yes, it should. I used it before without problem but the dropdown was different, the options where visible only after clicking the dropdown and it wasn't necessary to hold ctrl for multiple choice. Maybe that's the problem.
                – Guy
                Nov 22 at 11:24















              up vote
              0
              down vote



              accepted










              Select class can handle multiple choice dropdown. It even checks if the dropdown is multiple choice when using DeselectAll(). From github



              public void DeselectAll()
              {
              if (!this.IsMultiple)
              {
              throw new InvalidOperationException("You may only deselect all options if multi-select is supported");
              }

              foreach (IWebElement option in this.Options)
              {
              SetSelected(option, false);
              }
              }

              private static void SetSelected(IWebElement option, bool select)
              {
              bool isSelected = option.Selected;
              if ((!isSelected && select) || (isSelected && !select))
              {
              option.Click();
              }
              }


              When you click on the first select option without pressing the control key this option remains selected but all the other options are being deselected, so the click is actually not performed on the rest of the options since both isSelected and select are false in SetSelected.



              The solution is either implement your own DeselectAll() as suggested in your question, or select the first option from the dropdown (which will automatically deselect all the other options) and then deselect this option using control.






              share|improve this answer





















              • So DeselectAll() indeed does not deselect all. But it should according to the method name and the documentation: "Clear all selected entries."
                – Jack Miller
                Nov 22 at 10:22












              • @JackMiller Yes, it should. I used it before without problem but the dropdown was different, the options where visible only after clicking the dropdown and it wasn't necessary to hold ctrl for multiple choice. Maybe that's the problem.
                – Guy
                Nov 22 at 11:24













              up vote
              0
              down vote



              accepted







              up vote
              0
              down vote



              accepted






              Select class can handle multiple choice dropdown. It even checks if the dropdown is multiple choice when using DeselectAll(). From github



              public void DeselectAll()
              {
              if (!this.IsMultiple)
              {
              throw new InvalidOperationException("You may only deselect all options if multi-select is supported");
              }

              foreach (IWebElement option in this.Options)
              {
              SetSelected(option, false);
              }
              }

              private static void SetSelected(IWebElement option, bool select)
              {
              bool isSelected = option.Selected;
              if ((!isSelected && select) || (isSelected && !select))
              {
              option.Click();
              }
              }


              When you click on the first select option without pressing the control key this option remains selected but all the other options are being deselected, so the click is actually not performed on the rest of the options since both isSelected and select are false in SetSelected.



              The solution is either implement your own DeselectAll() as suggested in your question, or select the first option from the dropdown (which will automatically deselect all the other options) and then deselect this option using control.






              share|improve this answer












              Select class can handle multiple choice dropdown. It even checks if the dropdown is multiple choice when using DeselectAll(). From github



              public void DeselectAll()
              {
              if (!this.IsMultiple)
              {
              throw new InvalidOperationException("You may only deselect all options if multi-select is supported");
              }

              foreach (IWebElement option in this.Options)
              {
              SetSelected(option, false);
              }
              }

              private static void SetSelected(IWebElement option, bool select)
              {
              bool isSelected = option.Selected;
              if ((!isSelected && select) || (isSelected && !select))
              {
              option.Click();
              }
              }


              When you click on the first select option without pressing the control key this option remains selected but all the other options are being deselected, so the click is actually not performed on the rest of the options since both isSelected and select are false in SetSelected.



              The solution is either implement your own DeselectAll() as suggested in your question, or select the first option from the dropdown (which will automatically deselect all the other options) and then deselect this option using control.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 at 10:13









              Guy

              18.2k62149




              18.2k62149












              • So DeselectAll() indeed does not deselect all. But it should according to the method name and the documentation: "Clear all selected entries."
                – Jack Miller
                Nov 22 at 10:22












              • @JackMiller Yes, it should. I used it before without problem but the dropdown was different, the options where visible only after clicking the dropdown and it wasn't necessary to hold ctrl for multiple choice. Maybe that's the problem.
                – Guy
                Nov 22 at 11:24


















              • So DeselectAll() indeed does not deselect all. But it should according to the method name and the documentation: "Clear all selected entries."
                – Jack Miller
                Nov 22 at 10:22












              • @JackMiller Yes, it should. I used it before without problem but the dropdown was different, the options where visible only after clicking the dropdown and it wasn't necessary to hold ctrl for multiple choice. Maybe that's the problem.
                – Guy
                Nov 22 at 11:24
















              So DeselectAll() indeed does not deselect all. But it should according to the method name and the documentation: "Clear all selected entries."
              – Jack Miller
              Nov 22 at 10:22






              So DeselectAll() indeed does not deselect all. But it should according to the method name and the documentation: "Clear all selected entries."
              – Jack Miller
              Nov 22 at 10:22














              @JackMiller Yes, it should. I used it before without problem but the dropdown was different, the options where visible only after clicking the dropdown and it wasn't necessary to hold ctrl for multiple choice. Maybe that's the problem.
              – Guy
              Nov 22 at 11:24




              @JackMiller Yes, it should. I used it before without problem but the dropdown was different, the options where visible only after clicking the dropdown and it wasn't necessary to hold ctrl for multiple choice. Maybe that's the problem.
              – Guy
              Nov 22 at 11:24












              up vote
              2
              down vote













              You can for sure deselect these with



              browser.execute_script("[...document.querySelectorAll('[name=cars] option')].map(o => o.selected = false)")





              share|improve this answer



























                up vote
                2
                down vote













                You can for sure deselect these with



                browser.execute_script("[...document.querySelectorAll('[name=cars] option')].map(o => o.selected = false)")





                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  You can for sure deselect these with



                  browser.execute_script("[...document.querySelectorAll('[name=cars] option')].map(o => o.selected = false)")





                  share|improve this answer














                  You can for sure deselect these with



                  browser.execute_script("[...document.querySelectorAll('[name=cars] option')].map(o => o.selected = false)")






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 at 12:27

























                  answered Nov 22 at 9:56









                  pguardiario

                  35.7k979112




                  35.7k979112






















                      up vote
                      0
                      down vote













                      Even though I did not ask for code how to set the selection state, I'll post it anyways for anybody who encountered the same problem. This is C# code. You'll need NuGet packages Selenium.WebDriver and Selenium.Support. BrowserDriver is a member variable of my helper class which contains this method.



                      /// <summary>
                      /// Sets the selection state of <paramref name="selectElement"/>. All options specified by <paramref name="selectedOptions"/>
                      /// are select, all others are unselected.
                      /// </summary>
                      /// <param name="selectElement">HTML select element</param>
                      /// <param name="selectedOptions">options to be selected</param>
                      internal void SelectStateByText(OpenQA.Selenium.Support.UI.SelectElement selectElement, params string selectedOptions)
                      {
                      Assert.IsNotNull(selectElement);
                      Assert.IsNotNull(selectedOptions);
                      CollectionAssert.IsSubsetOf(selectedOptions, selectElement.Options.Select(o => o.Text).ToArray());
                      if (!selectElement.IsMultiple)
                      {
                      Assert.AreEqual(1, selectedOptions.Length);
                      selectElement.SelectByText(selectedOptions[0]);
                      }
                      else
                      {
                      var actions = new OpenQA.Selenium.Interactions.Actions(BrowserDriver);
                      actions.KeyDown(Keys.LeftControl);
                      foreach (var option in selectElement.Options)
                      {
                      if (selectedOptions.Contains(option.Text) && !option.Selected)
                      {
                      actions.Click(option);
                      }
                      else if (option.Selected)
                      {
                      actions.Click(option);
                      }
                      }
                      actions.KeyUp(Keys.LeftControl).Build().Perform();
                      }
                      }





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Even though I did not ask for code how to set the selection state, I'll post it anyways for anybody who encountered the same problem. This is C# code. You'll need NuGet packages Selenium.WebDriver and Selenium.Support. BrowserDriver is a member variable of my helper class which contains this method.



                        /// <summary>
                        /// Sets the selection state of <paramref name="selectElement"/>. All options specified by <paramref name="selectedOptions"/>
                        /// are select, all others are unselected.
                        /// </summary>
                        /// <param name="selectElement">HTML select element</param>
                        /// <param name="selectedOptions">options to be selected</param>
                        internal void SelectStateByText(OpenQA.Selenium.Support.UI.SelectElement selectElement, params string selectedOptions)
                        {
                        Assert.IsNotNull(selectElement);
                        Assert.IsNotNull(selectedOptions);
                        CollectionAssert.IsSubsetOf(selectedOptions, selectElement.Options.Select(o => o.Text).ToArray());
                        if (!selectElement.IsMultiple)
                        {
                        Assert.AreEqual(1, selectedOptions.Length);
                        selectElement.SelectByText(selectedOptions[0]);
                        }
                        else
                        {
                        var actions = new OpenQA.Selenium.Interactions.Actions(BrowserDriver);
                        actions.KeyDown(Keys.LeftControl);
                        foreach (var option in selectElement.Options)
                        {
                        if (selectedOptions.Contains(option.Text) && !option.Selected)
                        {
                        actions.Click(option);
                        }
                        else if (option.Selected)
                        {
                        actions.Click(option);
                        }
                        }
                        actions.KeyUp(Keys.LeftControl).Build().Perform();
                        }
                        }





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Even though I did not ask for code how to set the selection state, I'll post it anyways for anybody who encountered the same problem. This is C# code. You'll need NuGet packages Selenium.WebDriver and Selenium.Support. BrowserDriver is a member variable of my helper class which contains this method.



                          /// <summary>
                          /// Sets the selection state of <paramref name="selectElement"/>. All options specified by <paramref name="selectedOptions"/>
                          /// are select, all others are unselected.
                          /// </summary>
                          /// <param name="selectElement">HTML select element</param>
                          /// <param name="selectedOptions">options to be selected</param>
                          internal void SelectStateByText(OpenQA.Selenium.Support.UI.SelectElement selectElement, params string selectedOptions)
                          {
                          Assert.IsNotNull(selectElement);
                          Assert.IsNotNull(selectedOptions);
                          CollectionAssert.IsSubsetOf(selectedOptions, selectElement.Options.Select(o => o.Text).ToArray());
                          if (!selectElement.IsMultiple)
                          {
                          Assert.AreEqual(1, selectedOptions.Length);
                          selectElement.SelectByText(selectedOptions[0]);
                          }
                          else
                          {
                          var actions = new OpenQA.Selenium.Interactions.Actions(BrowserDriver);
                          actions.KeyDown(Keys.LeftControl);
                          foreach (var option in selectElement.Options)
                          {
                          if (selectedOptions.Contains(option.Text) && !option.Selected)
                          {
                          actions.Click(option);
                          }
                          else if (option.Selected)
                          {
                          actions.Click(option);
                          }
                          }
                          actions.KeyUp(Keys.LeftControl).Build().Perform();
                          }
                          }





                          share|improve this answer












                          Even though I did not ask for code how to set the selection state, I'll post it anyways for anybody who encountered the same problem. This is C# code. You'll need NuGet packages Selenium.WebDriver and Selenium.Support. BrowserDriver is a member variable of my helper class which contains this method.



                          /// <summary>
                          /// Sets the selection state of <paramref name="selectElement"/>. All options specified by <paramref name="selectedOptions"/>
                          /// are select, all others are unselected.
                          /// </summary>
                          /// <param name="selectElement">HTML select element</param>
                          /// <param name="selectedOptions">options to be selected</param>
                          internal void SelectStateByText(OpenQA.Selenium.Support.UI.SelectElement selectElement, params string selectedOptions)
                          {
                          Assert.IsNotNull(selectElement);
                          Assert.IsNotNull(selectedOptions);
                          CollectionAssert.IsSubsetOf(selectedOptions, selectElement.Options.Select(o => o.Text).ToArray());
                          if (!selectElement.IsMultiple)
                          {
                          Assert.AreEqual(1, selectedOptions.Length);
                          selectElement.SelectByText(selectedOptions[0]);
                          }
                          else
                          {
                          var actions = new OpenQA.Selenium.Interactions.Actions(BrowserDriver);
                          actions.KeyDown(Keys.LeftControl);
                          foreach (var option in selectElement.Options)
                          {
                          if (selectedOptions.Contains(option.Text) && !option.Selected)
                          {
                          actions.Click(option);
                          }
                          else if (option.Selected)
                          {
                          actions.Click(option);
                          }
                          }
                          actions.KeyUp(Keys.LeftControl).Build().Perform();
                          }
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 22 at 10:17









                          Jack Miller

                          1,61511832




                          1,61511832






















                              up vote
                              -1
                              down vote













                              I have verified your usecase of using the deselect_all() method through the Selenium Python Client and it seems to work perfecto.



                              deselect_all()



                              deselect_all() method clears all selected entries. This is only valid when the SELECT supports multiple selections. throws NotImplementedError If the SELECT does not support multiple selections.





                              Illustration



                              Note: As you have mentioned in your question I have also simulated the selection of all the items manually





                              • Code Block:



                                from selenium import webdriver
                                from selenium.webdriver.common.by import By
                                from selenium.webdriver.support.ui import WebDriverWait
                                from selenium.webdriver.support import expected_conditions as EC
                                from selenium.webdriver.support.ui import Select
                                import time

                                options = webdriver.ChromeOptions()
                                options.add_argument("start-maximized")
                                options.add_argument('disable-infobars')
                                driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
                                driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple')
                                WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframeResult")))
                                select_cars = Select(driver.find_element_by_css_selector("select[name='cars']"))
                                time.sleep(5) # Timeframe to Manually select all the items
                                select_cars.deselect_all()


                              • Browser Snapshot:



                              deselect_all.gif






                              share|improve this answer























                              • Very interesting! In the Python code I see that the same things happens: .Click() of the element(s) is called. Is it possible that you call your Python script using a shortcut which includes CTRL?
                                – Jack Miller
                                Nov 22 at 11:22










                              • @JackMiller Much more interesting is the fact that you find this answer interesting yet this answer is the only downvoted answer among several :)
                                – DebanjanB
                                Nov 23 at 7:55















                              up vote
                              -1
                              down vote













                              I have verified your usecase of using the deselect_all() method through the Selenium Python Client and it seems to work perfecto.



                              deselect_all()



                              deselect_all() method clears all selected entries. This is only valid when the SELECT supports multiple selections. throws NotImplementedError If the SELECT does not support multiple selections.





                              Illustration



                              Note: As you have mentioned in your question I have also simulated the selection of all the items manually





                              • Code Block:



                                from selenium import webdriver
                                from selenium.webdriver.common.by import By
                                from selenium.webdriver.support.ui import WebDriverWait
                                from selenium.webdriver.support import expected_conditions as EC
                                from selenium.webdriver.support.ui import Select
                                import time

                                options = webdriver.ChromeOptions()
                                options.add_argument("start-maximized")
                                options.add_argument('disable-infobars')
                                driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
                                driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple')
                                WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframeResult")))
                                select_cars = Select(driver.find_element_by_css_selector("select[name='cars']"))
                                time.sleep(5) # Timeframe to Manually select all the items
                                select_cars.deselect_all()


                              • Browser Snapshot:



                              deselect_all.gif






                              share|improve this answer























                              • Very interesting! In the Python code I see that the same things happens: .Click() of the element(s) is called. Is it possible that you call your Python script using a shortcut which includes CTRL?
                                – Jack Miller
                                Nov 22 at 11:22










                              • @JackMiller Much more interesting is the fact that you find this answer interesting yet this answer is the only downvoted answer among several :)
                                – DebanjanB
                                Nov 23 at 7:55













                              up vote
                              -1
                              down vote










                              up vote
                              -1
                              down vote









                              I have verified your usecase of using the deselect_all() method through the Selenium Python Client and it seems to work perfecto.



                              deselect_all()



                              deselect_all() method clears all selected entries. This is only valid when the SELECT supports multiple selections. throws NotImplementedError If the SELECT does not support multiple selections.





                              Illustration



                              Note: As you have mentioned in your question I have also simulated the selection of all the items manually





                              • Code Block:



                                from selenium import webdriver
                                from selenium.webdriver.common.by import By
                                from selenium.webdriver.support.ui import WebDriverWait
                                from selenium.webdriver.support import expected_conditions as EC
                                from selenium.webdriver.support.ui import Select
                                import time

                                options = webdriver.ChromeOptions()
                                options.add_argument("start-maximized")
                                options.add_argument('disable-infobars')
                                driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
                                driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple')
                                WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframeResult")))
                                select_cars = Select(driver.find_element_by_css_selector("select[name='cars']"))
                                time.sleep(5) # Timeframe to Manually select all the items
                                select_cars.deselect_all()


                              • Browser Snapshot:



                              deselect_all.gif






                              share|improve this answer














                              I have verified your usecase of using the deselect_all() method through the Selenium Python Client and it seems to work perfecto.



                              deselect_all()



                              deselect_all() method clears all selected entries. This is only valid when the SELECT supports multiple selections. throws NotImplementedError If the SELECT does not support multiple selections.





                              Illustration



                              Note: As you have mentioned in your question I have also simulated the selection of all the items manually





                              • Code Block:



                                from selenium import webdriver
                                from selenium.webdriver.common.by import By
                                from selenium.webdriver.support.ui import WebDriverWait
                                from selenium.webdriver.support import expected_conditions as EC
                                from selenium.webdriver.support.ui import Select
                                import time

                                options = webdriver.ChromeOptions()
                                options.add_argument("start-maximized")
                                options.add_argument('disable-infobars')
                                driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')
                                driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple')
                                WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframeResult")))
                                select_cars = Select(driver.find_element_by_css_selector("select[name='cars']"))
                                time.sleep(5) # Timeframe to Manually select all the items
                                select_cars.deselect_all()


                              • Browser Snapshot:



                              deselect_all.gif







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 22 at 10:40

























                              answered Nov 22 at 10:35









                              DebanjanB

                              37.4k73373




                              37.4k73373












                              • Very interesting! In the Python code I see that the same things happens: .Click() of the element(s) is called. Is it possible that you call your Python script using a shortcut which includes CTRL?
                                – Jack Miller
                                Nov 22 at 11:22










                              • @JackMiller Much more interesting is the fact that you find this answer interesting yet this answer is the only downvoted answer among several :)
                                – DebanjanB
                                Nov 23 at 7:55


















                              • Very interesting! In the Python code I see that the same things happens: .Click() of the element(s) is called. Is it possible that you call your Python script using a shortcut which includes CTRL?
                                – Jack Miller
                                Nov 22 at 11:22










                              • @JackMiller Much more interesting is the fact that you find this answer interesting yet this answer is the only downvoted answer among several :)
                                – DebanjanB
                                Nov 23 at 7:55
















                              Very interesting! In the Python code I see that the same things happens: .Click() of the element(s) is called. Is it possible that you call your Python script using a shortcut which includes CTRL?
                              – Jack Miller
                              Nov 22 at 11:22




                              Very interesting! In the Python code I see that the same things happens: .Click() of the element(s) is called. Is it possible that you call your Python script using a shortcut which includes CTRL?
                              – Jack Miller
                              Nov 22 at 11:22












                              @JackMiller Much more interesting is the fact that you find this answer interesting yet this answer is the only downvoted answer among several :)
                              – DebanjanB
                              Nov 23 at 7:55




                              @JackMiller Much more interesting is the fact that you find this answer interesting yet this answer is the only downvoted answer among several :)
                              – DebanjanB
                              Nov 23 at 7:55


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427859%2fselenium-deselectall-not-working-for-html-select-with-multiple-attribute%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Contact image not getting when fetch all contact list from iPhone by CNContact

                              count number of partitions of a set with n elements into k subsets

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks