2021年4月7日星期三

Click through dropdown menu with selenium

I am trying to create flashcards on quizlet.com with selenium. If you visit, you will see a "Create" button (or just a "+" depending on window size) in the navbar, when you click this it turns into a dropdown menu with 3 more buttons: 'Study Set', 'Folder' and 'Class'. (I am trying to click Study Set)

First, I am not even sure If I need to have selenium click the first 'Create' button to access the 'Study Set' button or if I can just jump straight to the 'Study Set' button. Anyway, here is the html related to the 'Create' button and 'Study Set' button, respectively:

<button type="button" aria-label="Create"><span>Create</span>/button>  

and: (Note, all 3 buttons share the class 'UILink' and the first button is the 'Study Set' button and the comments are mine)

<div class="s1ovpdog"> <!-- going to grab this div first because theres 85 elements with the class 'UILink' on the page, grabbing this div cuts it to 3-->      <button class="UILink" type="button">  <!-- then going to grab this button-->          <div class="c1ap9d88">              <div class="iiekfr8">                  <div class="i1q3l8tw">                      <svg aria-label="sets" class="AssemblyIcon AssemblyIcon--medium" role="img"><noscript></noscript>                      <use xlink:href="#sets"></use><noscript></noscript></svg><span class="AssemblyMenuItem--title t1nsp0j0">Study set</span>                  </div>              </div>          </div>      </button>      <button class="UILink" type="button">          <div class="c1ap9d88">              <div class="iiekfr8">                  <div class="i1q3l8tw">                      <svg aria-label="folder" class="AssemblyIcon AssemblyIcon--medium" role="img"><noscript></noscript>                      <use xlink:href="#folder"></use><noscript></noscript></svg><span class="AssemblyMenuItem--title t1nsp0j0">Folder</span>                  </div>              </div>          </div>      </button>      <button class="UILink" type="button">          <div class="c1ap9d88">              <div class="iiekfr8">                  <div class="i1q3l8tw">                      <svg aria-label="class" class="AssemblyIcon AssemblyIcon--medium" role="img"><noscript></noscript>                      <use xlink:href="#class"></use><noscript></noscript></svg><span class="AssemblyMenuItem--title t1nsp0j0">Class</span>                  </div>              </div>          </div>      </button>  </div>  

Python code:

from selenium import webdriver  from selenium.webdriver.common.keys import Keys    driver = webdriver.Chrome('./chromedriver')  driver.get("https://quizlet.com")    # Code to log in to quizlet omitted...    # Grab and click first 'Create' button  create_deck_button = driver.find_element_by_xpath("//button[@aria-label='Create']") # only one element has this aria-label  create_deck_button.send_keys(Keys.RETURN) # doesn't display drop-down menu, makes me think something is wrong here but also does not throw an error  # Grab and click 'Study Set' button  div_containting_study_set_button = driver.find_element_by_class_name('s1ovpdog') # only one element has this class  study_set_button = div_containting_study_set_button.find_elements_by_class_name('UILink')[0] # returns 3 buttons, only need first one  study_set_button.send_keys(Keys.RETURN) # Error,ElementNotInteractableException  

When I run this, it throws

raise exception_class(message, screen, stacktrace)  selenium.common.exceptions.ElementNotInteractableException: Message: element not interac  table  

Though study_set_button should definitely be referencing a button element, I believe? Thanks for any help with this.

https://stackoverflow.com/questions/66996372/click-through-dropdown-menu-with-selenium April 08, 2021 at 10:02AM

没有评论:

发表评论