3

I have tried many things it can't seem to get it to work so I am posting this question to hopefully learn the simple method of selecting from a drop down menu in python.

I manage to open the drop down menu but how can I select a value (let's say 4 in this example) from the drop down?

Below is the code that opens the drop down:

#select adults
adults = driver.find_element_by_xpath("//*[@id='adults-number']").click()

Below is the html that consists of all the options in the drop down (the one highlighted is the value I want selected):

enter image description here

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
BruceyBandit
  • 3,978
  • 19
  • 72
  • 144

1 Answers1

3

Use the Select class and it's .select_by_visible_text() method:

from selenium.webdriver.support.select import Select

adults = Select(driver.find_element_by_id("adults-number"))
adults.select_by_visible_text("4")

Note that I've also replaced the "by xpath" with the simpler and more efficient "by id" locator type.

Working code (using your target website) to select the adults = 4:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.get("http://jet2.com")

wait = WebDriverWait(driver, 10)
adults_element = wait.until(EC.presence_of_element_located((By.ID, "adults-number")))

select = Select(adults_element)
select.select_by_visible_text("4")
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • No dice, comes with error stating chrome window is undefined. I tried this earlier (with both id and xpath) with the docs you provided me earlier but doesn't work strangely. This is actually a follow with the bounty where trying to select adults. – BruceyBandit Feb 07 '16 at 13:40
  • @BruceyBandit okay, let me experiment a bit. Thanks. – alecxe Feb 07 '16 at 13:42
  • It's working now. Actually the first code worked when I removed the driver.maximize_window(). That seems to be causing these dodgy errors. Is it because it doesn't work with firefox really well? – BruceyBandit Feb 07 '16 at 14:00
  • @BruceyBandit interesting, I've tried with Firefox, Chrome with and without maximize_window() call - works for me in all the cases. – alecxe Feb 07 '16 at 14:02
  • What's also weird is that if I keep the maximise command, when I run it, it opens up a new browser but with the skype web page first before heading towards jet2 page. Remove the maximise and it opens up new browser window and goes straight to jet2. If I change to point to Google Chrome, it errors straight away. – BruceyBandit Feb 07 '16 at 14:06
  • @BruceyBandit interesting, what error do you get in Chrome? – alecxe Feb 07 '16 at 14:38
  • Well it's stating chromedriver issue. I installed Chromedriver and placed it in my C:\Selenium\Chrome directory. I set that in the path and then I did this in the python script: `driver = webdriver.Chrome('/C:/Selenium/Chrome/') driver.get("http://www.jet2.com")`, but it still states chromedriver should be in path, which it is when I check again in system > environment variables > PATH – BruceyBandit Feb 07 '16 at 14:41
  • @BruceyBandit the chromedriver.exe should either be in PATH, or you should provide the complete path to it as a first argument to `webdriver.Chrome()`. Note that *path to chromedriver*, not path to chrome. Hope it helps. – alecxe Feb 07 '16 at 14:45
  • 'Chrome' is a folder, the PATH does point to it unless I suppose to write the PATH as `C:\Selenium\Chrome\chromedriver` or `driver = webdriver.Chrome('/C:/Selenium/Chrome/chromedriver')`? – BruceyBandit Feb 07 '16 at 15:03
  • Perfect :) Thank you. Completed the first page. Now onto the difficult second page. Hoping the calendar code you showed me can be manipulated to help to conquer the second page – BruceyBandit Feb 07 '16 at 15:29
  • @BruceyBandit sure, let me know if you need further help with that. Thanks. – alecxe Feb 07 '16 at 15:51
  • @alcexe Can't see to figure it out. Question is here with your old code if you wish to give it try: http://stackoverflow.com/questions/35256504/how-to-check-current-date-and-move-to-next-date – BruceyBandit Feb 07 '16 at 19:01