简体   繁体   中英

loop to select items consequently from drop down list in selenium python

There is a list of 4 items I need to loop between the 4 items using (select_by_index) and do a function called reserve() after selecting each item

My code is:

driver.get('https://foreupsoftware.com/index.php/booking/20290#/')
driver.maximize_window()

course=driver.find_element(By.ID,'schedule_select')
dropdown_1=Select(course)
dropdown_1.select_by_index(0)
    
course=driver.find_element(By.ID,'schedule_select')
dropdown_1=Select(course)
dropdown_1.select_by_index(1)

course=driver.find_element(By.ID,'schedule_select')
dropdown_1=Select(course)
dropdown_1.select_by_index(2)

course=driver.find_element(By.ID,'schedule_select')
dropdown_1=Select(course)
dropdown_1.select_by_index(3)


def reserve():     
    non_passholder_button=driver.find_element(By.XPATH,'//button[@class="btn btn-primary col-md-4 col-xs-12 col-md-offset-4"][contains(., "Non Passholder")]')
    non_passholder_button.click()

After analyzing the web page I found that the url is made up of options present in the dropdown. So first you can get all the options in the page and then you can go through each url which is constructed using the option.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

chrome_path = r"C:\Users\hpoddar\Desktop\Tools\chromedriver_win32\chromedriver.exe"

s = Service(chrome_path)
driver = webdriver.Chrome(service=s)
driver.get(url)
  
options = driver.execute_script("""
    var result = [];
    var all = document.querySelectorAll("option");
    for (var i=0, max=all.length; i < max; i++) {
        result.push(all[i].getAttribute('value'));
    }
    return result;
""")
  
for option in options:
    value_url = f'https://foreupsoftware.com/index.php/booking/20153/{option}#/teetimes/'
    driver.get(value_url)
    non_passholder_button = driver.find_element(by=By.XPATH, value="//button[contains(text(),'Non Passholder')]")
    non_passholder_button.click()
    # the page has been opened after the click 
    # DO YOUR STUFF HERE

EDIT : As discussed the OP wants the options to be clicked mandatorily and does not want to go over each page by constructing a url.

This is how you can do it

url = 'https://foreupsoftware.com/index.php/booking/20155/3782#/teetimes/'
driver.get(url)
option_index = 0
  
num_of_options = len(driver.find_elements(By.CSS_SELECTOR, '#schedule_select > option'))
  
while(option_index < num_of_options):
    facility = driver.find_element(By.ID, 'schedule_select')
    facility.click()
    
    drop_options = driver.find_elements(By.CSS_SELECTOR, '#schedule_select > option')
    drop_options[option_index].click()
    
    non_passholder_button = driver.find_element(by=By.XPATH, value="//button[contains(text(),'Non Passholder')]")
    non_passholder_button.click()
    
    # DO YOUR STUFFS HERE

    driver.get(url)
    option_index += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM