我试图选择2021年的下拉列表(https://www.theknot.com/registry/couplesearch)我不知道如何使用下拉列表。
# This code is working
typetextfirst = driver.find_element_by_id("couples-search-first-name")
typetextfirst.clear()
typetextfirst.send_keys(row["First"])
typetextlast = driver.find_element_by_id("couples-search-last-name")
typetextlast.clear()
typetextlast.send_keys(row["Last"])
typetextyear = driver.find_element_by_id("couples-search-year")
# None of these options work to populate the year
typetextyear.selectByIndex(1)
typetextyear.select_by_index(1)
typetextyear.selectByVisibleText("2021")
typetextyear.select_by_visible_text("2021")
# This code is working
typetextlast.send_keys(Keys.ENTER)
1条答案
按热度按时间t30tvxxf1#
页面不使用标准
dropdown
但它使用button
及ul
仿效dropdown
.这段代码对我来说很有用
Firefox
及Chrome
在LinuxMint上。首先我点击
button
开dropdown
用ul
后来我搜索li
使用所需的文本,然后单击它。因为它可能有文本
2021
有一些spaces
/tabs
/enters
(哪个浏览器不显示)所以我更喜欢contains
而不是=
```from selenium import webdriver
url = 'https://www.theknot.com/registry/couplesearch'
driver = webdriver.Firefox()
driver = webdriver.Chrome()
driver.get(url)
year_dropdown = driver.find_element_by_id("couples-search-year")
year_dropdown.click()
year = year_dropdown.find_element_by_xpath(".//li[contains(text(), '2021')]")
year = year_dropdown.find_element_by_xpath(".//li[text()='2021']")
year.click()