2

dropdown details

 <select name="fromMonth">
 <option value="1">January
 </option><option value="2">February
 </option><option value="3">March
 </option><option value="4">April
 </option><option value="5">May
 </option><option value="6">June
 </option><option value="7">July
 </option><option value="8">August
 </option><option selected="" value="9">September
 </option><option value="10">October
 </option><option value="11">November
 </option><option value="12">December
 </option></select>

I would like to print
January
February
so on...

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
thejesh
  • 83
  • 2
  • 7
  • Possible duplicate of [How can I get all elements from drop down list?](https://stackoverflow.com/questions/16768318/how-can-i-get-all-elements-from-drop-down-list) – NarendraR Sep 21 '18 at 04:09

4 Answers4

4

As per the HTML to print list of months present in dropdown you can use the following solution:

selectmonth = Select(driver.find_element_by_name('fromMonth'))
for option in selectmonth.options:
    print(option.text)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
3

This will help:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

url='' //of webpage

driver.maximize_window()

driver.get(url)

listofelements=driver.find_elements(By.XPATH,'//*[@name="fromMonth"]/option') //to take all elements matching xpath

for i in range(len(listofelements)):
    print(listofelements[i].text) //print all elements of list
thebadguy
  • 2,092
  • 1
  • 22
  • 31
-1

I think its "November" instead of "Noenter code herevember"

In JAVA you can use like this, You can apply the same logic in Python

//locate select drop down
WebElement monthsElement = driver.findElement(By.name("fromMonth"));

// use select class
Select monthsDrop = new Select(monthsElement);

//store the list all months in list using getOptions()
List<WebElement> allmonths = monthsDrop.getOptions();

//traverse and print all elements
for (WebElement tempmonth : allmonths) {
    System.out.println(tempmonth.getText());
}
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
  • 2
    This looks perfect since the dropdown element user shared has 'select' tag. However, you may want to port it to Python as per user's request. – Kireeti Annamaraj Sep 21 '18 at 06:19
-2
dropdown_data = driver.findElement(By.xpath("Xpath of the dropdown")) 

# which selects the dropdown

for i in range len(dropdown_data):
    print(dropdown_data[i].text)
Juhil Somaiya
  • 873
  • 7
  • 20
  • 2
    This doesn't look like valid Python. // should probably be #, the `for` statement will give a syntax error. If your intent was `for i in range(len(dropdown_data))`, it won't work either, because i will be an integer, so you can't do `i.text`. – Leo K Sep 21 '18 at 06:23