skip to Main Content

I am trying to get all elements within a drop down and print the total count. Here’s the code, which I have written for this.

import unittest
from selenium import  webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select

class dropdown(unittest.TestCase):
def setUp(self):
    self.driver=webdriver.Chrome()


def test_selectmethod(self):
    driver=self.driver
    driver.get("http://magento-demo.lexiconn.com/")
    driver.find_element_by_xpath("//*[@id='select-language']").click()

    dropdown=Select(driver.find_element_by_xpath("//*[@id='select-language']"))
    print str(len(dropdown)+ "products found"

    for i in dropdown:
        print(i.text)



def tearDown(self):
self.driver.close()

However, this throws error while printing str(len(dropdown).

2

Answers


  1. To display the count of dropdown items using selenium python you don’t even have to identify the <select> tag and you can use the following solution:

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.support.ui import Select
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      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("http://magento-demo.lexiconn.com/")
      count = len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//select[@id='select-language']//option"))))
      print(str(count) + " items found." )
      
    • Console Output:

      3 items found.
      

    But if you want to print the <options> within the <select> tag you have to identify the <select> tag and you can use the following solution:

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.support.ui import Select
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      
      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("http://magento-demo.lexiconn.com/")
      count = len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//select[@id='select-language']//option"))))
      print(str(count) + " items found." )
      dropdown = Select(driver.find_element_by_xpath("//*[@id='select-language']"))
      print("Items are: " )
      for i in dropdown.options:
          print(i.get_attribute('innerHTML'))
      
    • Console Output:

      3 items found.
      Items are: 
      English
      French
      German
      
    Login or Signup to reply.
  2. Try str(len(dropdown.options)).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search