How to select multiple options from multiselect element using Selenium-Python?












0















I am trying to select P0_ENGLISH, P1_ENGLISH, P5_ENGLISH from multiple select which has 10 options. I want to select only these 3 options.



HTML CODE:



<select multiple="" class="gwt-ListBox" style="height: 80px; width: 205px;">
<option title="Generic_Eng" value="Generic_Eng">Generic_Eng</option>
<option title="Generic_Hindi" value="Generic_Hindi">Generic_Hindi</option>
<option title="P0_English" value="P0_English">P0_English</option>
<option title="P0_Hindi" value="P0_Hindi">P0_Hindi</option>
<option title="P1_English" value="P1_English">P1_English</option>
<option title="P1_Hindi" value="P1_Hindi">P1_Hindi</option>
<option title="P4_English" value="P4_English">P4_English</option>
<option title="P4_Hindi" value="P4_Hindi">P4_Hindi</option>
<option title="P5_English" value="P5_English">P5_English</option>
<option title="P5_Hindi" value="P5_Hindi">P5_Hindi</option>
</select>


SELENIUM-PYTHON CODE:



queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox"))
queues.select_by_visible_text("P0_English")
time.sleep(3)
queues.select_by_visible_text("P1_English")
time.sleep(3)
queues.select_by_visible_text("P5_English"


I've tried using this code. With this code, I am able to select the first option i.e. "P0_ENGLISH". However, after selecting the first option I get an error:



selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document









share|improve this question

























  • try element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.xpath, title="P0_English"))

    – Dev
    Jan 19 at 16:40
















0















I am trying to select P0_ENGLISH, P1_ENGLISH, P5_ENGLISH from multiple select which has 10 options. I want to select only these 3 options.



HTML CODE:



<select multiple="" class="gwt-ListBox" style="height: 80px; width: 205px;">
<option title="Generic_Eng" value="Generic_Eng">Generic_Eng</option>
<option title="Generic_Hindi" value="Generic_Hindi">Generic_Hindi</option>
<option title="P0_English" value="P0_English">P0_English</option>
<option title="P0_Hindi" value="P0_Hindi">P0_Hindi</option>
<option title="P1_English" value="P1_English">P1_English</option>
<option title="P1_Hindi" value="P1_Hindi">P1_Hindi</option>
<option title="P4_English" value="P4_English">P4_English</option>
<option title="P4_Hindi" value="P4_Hindi">P4_Hindi</option>
<option title="P5_English" value="P5_English">P5_English</option>
<option title="P5_Hindi" value="P5_Hindi">P5_Hindi</option>
</select>


SELENIUM-PYTHON CODE:



queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox"))
queues.select_by_visible_text("P0_English")
time.sleep(3)
queues.select_by_visible_text("P1_English")
time.sleep(3)
queues.select_by_visible_text("P5_English"


I've tried using this code. With this code, I am able to select the first option i.e. "P0_ENGLISH". However, after selecting the first option I get an error:



selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document









share|improve this question

























  • try element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.xpath, title="P0_English"))

    – Dev
    Jan 19 at 16:40














0












0








0








I am trying to select P0_ENGLISH, P1_ENGLISH, P5_ENGLISH from multiple select which has 10 options. I want to select only these 3 options.



HTML CODE:



<select multiple="" class="gwt-ListBox" style="height: 80px; width: 205px;">
<option title="Generic_Eng" value="Generic_Eng">Generic_Eng</option>
<option title="Generic_Hindi" value="Generic_Hindi">Generic_Hindi</option>
<option title="P0_English" value="P0_English">P0_English</option>
<option title="P0_Hindi" value="P0_Hindi">P0_Hindi</option>
<option title="P1_English" value="P1_English">P1_English</option>
<option title="P1_Hindi" value="P1_Hindi">P1_Hindi</option>
<option title="P4_English" value="P4_English">P4_English</option>
<option title="P4_Hindi" value="P4_Hindi">P4_Hindi</option>
<option title="P5_English" value="P5_English">P5_English</option>
<option title="P5_Hindi" value="P5_Hindi">P5_Hindi</option>
</select>


SELENIUM-PYTHON CODE:



queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox"))
queues.select_by_visible_text("P0_English")
time.sleep(3)
queues.select_by_visible_text("P1_English")
time.sleep(3)
queues.select_by_visible_text("P5_English"


I've tried using this code. With this code, I am able to select the first option i.e. "P0_ENGLISH". However, after selecting the first option I get an error:



selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document









share|improve this question
















I am trying to select P0_ENGLISH, P1_ENGLISH, P5_ENGLISH from multiple select which has 10 options. I want to select only these 3 options.



HTML CODE:



<select multiple="" class="gwt-ListBox" style="height: 80px; width: 205px;">
<option title="Generic_Eng" value="Generic_Eng">Generic_Eng</option>
<option title="Generic_Hindi" value="Generic_Hindi">Generic_Hindi</option>
<option title="P0_English" value="P0_English">P0_English</option>
<option title="P0_Hindi" value="P0_Hindi">P0_Hindi</option>
<option title="P1_English" value="P1_English">P1_English</option>
<option title="P1_Hindi" value="P1_Hindi">P1_Hindi</option>
<option title="P4_English" value="P4_English">P4_English</option>
<option title="P4_Hindi" value="P4_Hindi">P4_Hindi</option>
<option title="P5_English" value="P5_English">P5_English</option>
<option title="P5_Hindi" value="P5_Hindi">P5_Hindi</option>
</select>


SELENIUM-PYTHON CODE:



queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox"))
queues.select_by_visible_text("P0_English")
time.sleep(3)
queues.select_by_visible_text("P1_English")
time.sleep(3)
queues.select_by_visible_text("P5_English"


I've tried using this code. With this code, I am able to select the first option i.e. "P0_ENGLISH". However, after selecting the first option I get an error:



selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document






python selenium selenium-webdriver webdriver multi-select






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 21:22









DebanjanB

41.3k83878




41.3k83878










asked Jan 19 at 16:14









SouravSourav

215




215













  • try element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.xpath, title="P0_English"))

    – Dev
    Jan 19 at 16:40



















  • try element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.xpath, title="P0_English"))

    – Dev
    Jan 19 at 16:40

















try element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.xpath, title="P0_English"))

– Dev
Jan 19 at 16:40





try element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.xpath, title="P0_English"))

– Dev
Jan 19 at 16:40












2 Answers
2






active

oldest

votes


















2














To select multiple options from a Multi Select element you can use ActionChains to mock Control Click as follows:



from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

myElemA = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P0_English']")
myElemB = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P1_English']")
myElemC = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P5_English']")
ActionChains(driver).key_down(Keys.CONTROL).click(myElemA).key_up(Keys.CONTROL).perform()
ActionChains(driver).key_down(Keys.CONTROL).click(myElemB).key_up(Keys.CONTROL).perform()
ActionChains(driver).key_down(Keys.CONTROL).click(myElemC).key_up(Keys.CONTROL).perform()





share|improve this answer































    1














    In the context of Selenium, a reference is stale when the reference is invalid, because the referenced element has been deleted, or outdated as the element has been detached and then attached by a client-side script. Without knowing the precise mechanics of the client script, there may be different solutions. The easiest is to attempt to reference the element again, i.e.



    queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
    queues.select_by_visible_text("P0_English")
    time.sleep(3)
    queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
    queues.select_by_visible_text("P1_English")
    time.sleep(3)
    queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
    queues.select_by_visible_text("P5_English")


    This assumes that the CSS selector stays the same after the selection list is reattached. There is also a possibility that the selector becomes invalid because the element has been either removed or its location has been changed. In the first case, you'd want to throw an exception and handle it appropriately and, in the second, find out what its new selector is going to be either empirically or by client-side script code analysis. More on the StaleElementReferenceException here.






    share|improve this answer























      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',
      autoActivateHeartbeat: false,
      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%2f54268992%2fhow-to-select-multiple-options-from-multiselect-element-using-selenium-python%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      To select multiple options from a Multi Select element you can use ActionChains to mock Control Click as follows:



      from selenium.webdriver.common.action_chains import ActionChains
      from selenium.webdriver.common.keys import Keys

      myElemA = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P0_English']")
      myElemB = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P1_English']")
      myElemC = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P5_English']")
      ActionChains(driver).key_down(Keys.CONTROL).click(myElemA).key_up(Keys.CONTROL).perform()
      ActionChains(driver).key_down(Keys.CONTROL).click(myElemB).key_up(Keys.CONTROL).perform()
      ActionChains(driver).key_down(Keys.CONTROL).click(myElemC).key_up(Keys.CONTROL).perform()





      share|improve this answer




























        2














        To select multiple options from a Multi Select element you can use ActionChains to mock Control Click as follows:



        from selenium.webdriver.common.action_chains import ActionChains
        from selenium.webdriver.common.keys import Keys

        myElemA = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P0_English']")
        myElemB = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P1_English']")
        myElemC = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P5_English']")
        ActionChains(driver).key_down(Keys.CONTROL).click(myElemA).key_up(Keys.CONTROL).perform()
        ActionChains(driver).key_down(Keys.CONTROL).click(myElemB).key_up(Keys.CONTROL).perform()
        ActionChains(driver).key_down(Keys.CONTROL).click(myElemC).key_up(Keys.CONTROL).perform()





        share|improve this answer


























          2












          2








          2







          To select multiple options from a Multi Select element you can use ActionChains to mock Control Click as follows:



          from selenium.webdriver.common.action_chains import ActionChains
          from selenium.webdriver.common.keys import Keys

          myElemA = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P0_English']")
          myElemB = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P1_English']")
          myElemC = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P5_English']")
          ActionChains(driver).key_down(Keys.CONTROL).click(myElemA).key_up(Keys.CONTROL).perform()
          ActionChains(driver).key_down(Keys.CONTROL).click(myElemB).key_up(Keys.CONTROL).perform()
          ActionChains(driver).key_down(Keys.CONTROL).click(myElemC).key_up(Keys.CONTROL).perform()





          share|improve this answer













          To select multiple options from a Multi Select element you can use ActionChains to mock Control Click as follows:



          from selenium.webdriver.common.action_chains import ActionChains
          from selenium.webdriver.common.keys import Keys

          myElemA = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P0_English']")
          myElemB = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P1_English']")
          myElemC = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P5_English']")
          ActionChains(driver).key_down(Keys.CONTROL).click(myElemA).key_up(Keys.CONTROL).perform()
          ActionChains(driver).key_down(Keys.CONTROL).click(myElemB).key_up(Keys.CONTROL).perform()
          ActionChains(driver).key_down(Keys.CONTROL).click(myElemC).key_up(Keys.CONTROL).perform()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 21:28









          DebanjanBDebanjanB

          41.3k83878




          41.3k83878

























              1














              In the context of Selenium, a reference is stale when the reference is invalid, because the referenced element has been deleted, or outdated as the element has been detached and then attached by a client-side script. Without knowing the precise mechanics of the client script, there may be different solutions. The easiest is to attempt to reference the element again, i.e.



              queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
              queues.select_by_visible_text("P0_English")
              time.sleep(3)
              queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
              queues.select_by_visible_text("P1_English")
              time.sleep(3)
              queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
              queues.select_by_visible_text("P5_English")


              This assumes that the CSS selector stays the same after the selection list is reattached. There is also a possibility that the selector becomes invalid because the element has been either removed or its location has been changed. In the first case, you'd want to throw an exception and handle it appropriately and, in the second, find out what its new selector is going to be either empirically or by client-side script code analysis. More on the StaleElementReferenceException here.






              share|improve this answer




























                1














                In the context of Selenium, a reference is stale when the reference is invalid, because the referenced element has been deleted, or outdated as the element has been detached and then attached by a client-side script. Without knowing the precise mechanics of the client script, there may be different solutions. The easiest is to attempt to reference the element again, i.e.



                queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
                queues.select_by_visible_text("P0_English")
                time.sleep(3)
                queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
                queues.select_by_visible_text("P1_English")
                time.sleep(3)
                queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
                queues.select_by_visible_text("P5_English")


                This assumes that the CSS selector stays the same after the selection list is reattached. There is also a possibility that the selector becomes invalid because the element has been either removed or its location has been changed. In the first case, you'd want to throw an exception and handle it appropriately and, in the second, find out what its new selector is going to be either empirically or by client-side script code analysis. More on the StaleElementReferenceException here.






                share|improve this answer


























                  1












                  1








                  1







                  In the context of Selenium, a reference is stale when the reference is invalid, because the referenced element has been deleted, or outdated as the element has been detached and then attached by a client-side script. Without knowing the precise mechanics of the client script, there may be different solutions. The easiest is to attempt to reference the element again, i.e.



                  queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
                  queues.select_by_visible_text("P0_English")
                  time.sleep(3)
                  queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
                  queues.select_by_visible_text("P1_English")
                  time.sleep(3)
                  queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
                  queues.select_by_visible_text("P5_English")


                  This assumes that the CSS selector stays the same after the selection list is reattached. There is also a possibility that the selector becomes invalid because the element has been either removed or its location has been changed. In the first case, you'd want to throw an exception and handle it appropriately and, in the second, find out what its new selector is going to be either empirically or by client-side script code analysis. More on the StaleElementReferenceException here.






                  share|improve this answer













                  In the context of Selenium, a reference is stale when the reference is invalid, because the referenced element has been deleted, or outdated as the element has been detached and then attached by a client-side script. Without knowing the precise mechanics of the client script, there may be different solutions. The easiest is to attempt to reference the element again, i.e.



                  queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
                  queues.select_by_visible_text("P0_English")
                  time.sleep(3)
                  queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
                  queues.select_by_visible_text("P1_English")
                  time.sleep(3)
                  queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
                  queues.select_by_visible_text("P5_English")


                  This assumes that the CSS selector stays the same after the selection list is reattached. There is also a possibility that the selector becomes invalid because the element has been either removed or its location has been changed. In the first case, you'd want to throw an exception and handle it appropriately and, in the second, find out what its new selector is going to be either empirically or by client-side script code analysis. More on the StaleElementReferenceException here.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 19 at 16:40









                  adqadq

                  213




                  213






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54268992%2fhow-to-select-multiple-options-from-multiselect-element-using-selenium-python%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

                      Homophylophilia

                      Updating UILabel text programmatically using a function

                      Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage