How do I create a loop within my selenium python script to select every option шт dropdown menu



  • Im new to python, or coding for that matter... Currently I have a python script to select one option within the dropdown menu, but I would like my script to repeat each time and select the next option. Theres about 50 different options within the dropdown.

    l1  = "Hong Kong, China (Chrome, Canary, Firefox)"
    
    urlTextBox          = "url"
    dropdownOption      = "location"
    submitBtn           = ".//*[@kalena homeBtn             = ".//*[@id='nav']/li[1]/a"
    
    urlTextBoxElement = WebDriverWait(driver, 10).\
        until(lambda driver: driver.find_element_by_id(urlTextBox))
    
    dropdownOptionElement = WebDriverWait(driver, 10).\
        until(lambda driver: driver.find_element_by_id(dropdownOption))
    
    submitBtnElement = WebDriverWait(driver, 10).\
        until(lambda driver: driver.find_element_by_xpath(submitBtn))
    
    urlTextBoxElement.send_keys(webTeamPage)
    Select(dropdownOptionElement).select_by_visible_text(l1)
    submitBtnElement.click()
    time.sleep(3)
    homeBtnElement = WebDriverWait(driver, 10).\
        until(lambda driver: driver.find_element_by_xpath(homeBtn))
    


  • I was able to answer my own question, Please see script below:

        urlTextBox          = "url"
        dropdownOption      = "location"
        submitBtn           = ".//*[@kalena     homeBtn             = ".//*[@id='nav']/li[1]/a"
        webTeamPage = "personalwebpage.com"
    
    
        select = driver.find_element_by_id(dropdownOption)  #get the select element
        options = select.find_elements_by_tag_name("option") #get all the options into a list
        optionsList = []
    
        for option in options: #iterate over the options, place attribute value in list
    
            optionsList.append(option.get_attribute("value"))
    
    
        for optionValue in optionsList:
    
            urlTextBoxElement = WebDriverWait(driver, 10).\
                until(lambda driver: driver.find_element_by_id(urlTextBox))
    
            dropdownOptionElement = WebDriverWait(driver, 10).\
                until(lambda driver: driver.find_element_by_id(dropdownOption))
    
            submitBtnElement = WebDriverWait(driver, 10).\
                until(lambda driver: driver.find_element_by_xpath(submitBtn))
    
            driver.find_element_by_id(urlTextBox).clear()
            urlTextBoxElement.send_keys(webTeamPage)
            submitBtnElement.click()
            time.sleep(3)
            homeBtnElement = WebDriverWait(driver, 10).\
            until(lambda driver: driver.find_element_by_xpath(homeBtn))
            homeBtnElement.click()
            print "starting loop on option %s" % optionValue
    
    
            select = Select(driver.find_element_by_id(dropdownOption))
            select.select_by_value(optionValue)
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2