从硒的下拉菜单中选择
Selecting From the drop downs in selenium
我正在尝试使用 for 循环在下拉列表中输入 3 个值,但是我无法注入数据。我正在使用 Select class,因为下拉菜单是静态的,下面是我正在实现的快照和代码。你能帮我看看我做错了什么吗?另外,当我为所有三个下拉框输入“7 岁”的值时,但是如果我还想添加一些其他值,我该如何在循环中也这样做?
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()
你的错误在这一行:
dropdown = Select(agelist)
应该是
dropdown = Select(button)
那里。
此外,无需执行 button.click()
.
所以,而不是
for button in agelist:
time.sleep(0.7)
dropdown = Select(agelist)
selectchilderen = dropdown.select_by_visible_text("7 years old")
button.click()
应该是
for button in agelist:
time.sleep(0.7)
dropdown = Select(button)
selectchilderen = dropdown.select_by_visible_text("7 years old")
关于在下拉列表中选择变化的值,例如,您可以将循环索引添加到您正在选择的值,如下所示:
for index, button in enumerate(agelist):
time.sleep(0.7)
dropdown = Select(button)
selectchilderen = dropdown.select_by_visible_text(str(7 + index) + " years old")
我正在尝试使用 for 循环在下拉列表中输入 3 个值,但是我无法注入数据。我正在使用 Select class,因为下拉菜单是静态的,下面是我正在实现的快照和代码。你能帮我看看我做错了什么吗?另外,当我为所有三个下拉框输入“7 岁”的值时,但是如果我还想添加一些其他值,我该如何在循环中也这样做?
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()
你的错误在这一行:
dropdown = Select(agelist)
应该是
dropdown = Select(button)
那里。
此外,无需执行 button.click()
.
所以,而不是
for button in agelist:
time.sleep(0.7)
dropdown = Select(agelist)
selectchilderen = dropdown.select_by_visible_text("7 years old")
button.click()
应该是
for button in agelist:
time.sleep(0.7)
dropdown = Select(button)
selectchilderen = dropdown.select_by_visible_text("7 years old")
关于在下拉列表中选择变化的值,例如,您可以将循环索引添加到您正在选择的值,如下所示:
for index, button in enumerate(agelist):
time.sleep(0.7)
dropdown = Select(button)
selectchilderen = dropdown.select_by_visible_text(str(7 + index) + " years old")