简体   繁体   中英

Choosing an option in a dynamic list with Python

I'm completely new to Python and selenium but have been trying to create a script to book a tee time at my golf club.

I have been able to get the script to login, pick the correct time and date and add my self to the booking.

Where I am getting stuck is trying to add other 'members' to the booking. Once I have added my self you need to click the 'add members' button which opens a modal window with a dynamic list that you have to start typing in the name and it auto suggests as you type. I can get the script to type in my desired name but not select it from the list.

If I do this process manaully I'm able to type the name and hit the down arrow key and return twice, to select and confirm (close the modal). I have tried using.send_keys but nothing seems to work.

Below is the code I've used with email, password dummy content.

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time 


driver = webdriver.Chrome()
driver.get("https://www.manorgolfclub.net/members/login")

# Your Manor credentials
m_name = "blank@blank.com"
m_pass = "Blank123"
ryan = "Ryan Smith"

# Fill credentials
driver.find_element_by_id ('username').send_keys(m_name)
driver.find_element_by_id ('password').send_keys(m_pass)
driver.find_element_by_xpath ("//button[contains(text(),'Login')]").click();
driver.get ('https://www.manorgolfclub.net/members/book/golfers?date=07-03-2022&time=07:46')
driver.find_element_by_xpath ('html/body/div[2]/section[1]/div/form/div[2]/div[1]/div[1]/div/a[1]').click();
driver.find_element_by_id ('addMemberInput').send_keys(ryan)
driver.find_element_by_id ('addMemberInput').send_keys(Keys.DOWN)
# driver.quit()

I have added a screenshot below which shows the inspector panel in Chrome, just above the highlighted row is 'tt-open- this only appears when I start to type the name and as soon as I click off it goes again. Screenshot of inspector panel

Any help would be greatl appreciate, please let me know if I can supply anything else to help!

Try to use the selenium's ActionChain. Here's an exemple.

from selenium.webdriver import ActionChains

actions = ActionChains(driver)
actions.send_keys(Keys.DOWN).perform()

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