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
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:
Console Output:
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:
Console Output:
Try
str(len(dropdown.options))
.