Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting From the drop downs in selenium

I am trying to enter values 3 values in the drop down using for loop however I am unable to inject the data. I am using a Select class as the drop down is static below is the snap shot and code which I am implementing. Can you please help me where I am doing wrong. Also as I am entering the value as "7 years old" for all the three drop down boxes however if I want to add some other values as well how can I do that as well in the loop?

enter image description here

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

driver = webdriver.Chrome(executable_path='C:\chromedriver')
wait = WebDriverWait(driver, 15)

driver.get("https://www.booking.com/")
driver.maximize_window()
wait.until(ec.visibility_of_element_located((By.XPATH, "//input[@placeholder='Where are you going?']"))).send_keys("Jaisalmer")
wait.until(ec.visibility_of_element_located((By.XPATH, "//*[contains(@data-label,'Suryagarh ')]"))).click()

for i in range(3):
    time.sleep(0.5)
    wait.until(ec.visibility_of_element_located((By.XPATH, "//div[@class='bui-calendar__control bui-calendar__control--next']"))).click()


#select the checkin date
wait.until(ec.visibility_of_element_located((By.XPATH, "//span[@aria-label='22 June 2022']"))).click()
wait.until(ec.visibility_of_element_located((By.XPATH, "//span[@aria-label='25 July 2022']"))).click()

#class name = xp__guests__count

wait.until(ec.visibility_of_element_located((By.CLASS_NAME, "xp__guests__count"))).click()

#Increase number of adults 

adultaddition = wait.until(ec.visibility_of_element_located((By.XPATH,"//button[@aria-label='Increase number of Adults']")))

for _ in range(7):
    adultaddition.click()

#Increase number of childern

for _ in range(3):
    time.sleep(0.7)
    wait.until(ec.visibility_of_element_located((By.XPATH,"//button[@aria-label='Increase number of Children']"))).click()


#adding children ages

agelist = driver.find_elements(By.XPATH, "//select[@name='age']")

for button in agelist:
    time.sleep(0.7)
    dropdown = Select(agelist)
    selectchilderen = dropdown.select_by_visible_text("7 years old")
    button.click()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your mistake here is in this line:

dropdown = Select(agelist)

It should be

dropdown = Select(button)

there.
Also, no need to perform button.click().
So, instead of

for button in agelist:
    time.sleep(0.7)
    dropdown = Select(agelist)
    selectchilderen = dropdown.select_by_visible_text("7 years old")
    button.click()

It should be

for button in agelist:
    time.sleep(0.7)
    dropdown = Select(button)
    selectchilderen = dropdown.select_by_visible_text("7 years old")

As about selecting changing values in the drop list, you can for example add the loop index to the value you are selecting, like the following:

for index, button in enumerate(agelist):
    time.sleep(0.7)
    dropdown = Select(button)
    selectchilderen = dropdown.select_by_visible_text(str(7 + index) + " years old")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading