2

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).

6
  • I am getting error in these line only print str(len(dropdown)+ "products found" Commented Jul 9, 2018 at 8:25
  • This code dropdown=Select(driver.find_element_by_xpath("//*[@id='select-language']")) should return a select element. You have to get all the options element within select element using findelements (in C#, use equivalent for python). Commented Jul 9, 2018 at 8:40
  • so instead of find element have to use find elements right? Commented Jul 9, 2018 at 9:17
  • but still facing the same problem Commented Jul 9, 2018 at 9:31
  • When you use FindElements, it return a ReadOnlyCollection<IwebElements>, on which you should be able to use .count() to get the count. If it returns zero, then xpath may have to modify to get correct value. Commented Jul 9, 2018 at 10:15

2 Answers 2

2

Try str(len(dropdown.options)).

Sign up to request clarification or add additional context in comments.

Comments

0

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:\Utility\BrowserDrivers\chromedriver.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:\Utility\BrowserDrivers\chromedriver.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
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.