如何使用Selenium和Python从没有任何ID属性的html选择下拉列表中选择选项

yb3bgrhw  于 2023-02-08  发布在  Python
关注(0)|答案(4)|浏览(271)

我尝试使用Selenium Select方法从下拉菜单中选择一个值,但我无法正确定位正确的元素,如其他Stackoverflow帖子所示,因为我的元素缺少ID,如其他Stackoverflow帖子所示。我尝试使用Xpath,Class_Name和CSS_Selector作为替代,但我一直得到错误"无法定位元素"。
这是HTML:

<select class="lf-simpelselect lf-bottom-margin" data-bind="options: activiteitenOptions, value: activiteit, optionsText: 'Naam', optionsValue: 'Id', visible: toonActiviteiten()" style=""><option value="0bdae2c9-6ebc-4396-b3bb-0e587f9bbe86">Latin Jam</option><option value="c18aef82-be4d-4881-ae96-28aa72161c17">Kiddo Swing 6-8 jr</option><option value="8cbe6618-68cd-47f1-8a79-29db71498292">Ballet </option><option value="8d03eb2f-02ea-434d-8520-30b2f02e18f1">Pilates</option><option value="30358ec5-9751-4b13-8514-4db7b541c540">Gold 40+</option><option value="b811c4be-703b-4193-8f60-57bdb574a60a">Feminine</option><option value="2c6b4f41-6953-4347-99b9-70c33f48da07">City Jam</option><option value="696ba3a2-a453-4550-9cbe-7a00d46dfb14">Dojo</option><option value="fc8b9e43-401f-4e38-8d0b-8afba5f1226b">Power Jam</option><option value="7192a0d5-65b1-43a0-80d6-987b90cf505c">Dans Mix 9 - 12 jr</option><option value="820cf6c6-c9a6-4759-939e-988c646a69f7">Barre Workout</option><option value="e9bfd01d-b183-483a-b809-b2d9dd62354d">Latin House 18+</option><option value="505e1c62-9e68-4569-a7ef-b6566ff1b56d">Afro</option><option value="e4899faa-969e-4953-af1f-bc7eba080bec">Hip Hop</option><option value="796f9e98-f54c-486c-bc62-e6a2e74a5f45">Contemporary Flow </option><option value="88845fb5-e1ab-418d-ba3c-ed1131821835">Modern</option><option value="cb7fa275-b034-4f04-8265-ef747fe73179">Dancehall</option></select>

我尝试使用Select()更改该值的代码如下所示:

url = "https://prd.mylogifit.com/logifitweb/genericwidgetbyid.html?a=Touchee&widgetId=73f1f696-5063-4fb7-9a8d-c588ef149eb4"

browser = webdriver.Chrome(executable_path='./chromedriver')
browser.get(url)

# Select lesson type (afro)
lesson_droplist = Select(browser.find_element(By.CLASS_NAME, 'lf-simpelselect lf-bottom-margin'))
lesson_droplist.select_by_value("Afro")

如何使用Select方法将下拉值从Hip-Hop更改为Afro?

oprakyz7

oprakyz71#

要选择文本为Pilates<option>,因为所需元素是***dynamic***元素,您需要为element_to_be_clickable()引入WebDriverWait,并且可以使用以下locator strategies之一:

  • 使用 * CSS选择器 *:
driver.get("https://prd.mylogifit.com/logifitweb/genericwidgetbyid.html?a=Touchee&widgetId=73f1f696-5063-4fb7-9a8d-c588ef149eb4")
Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.lf-simpelselect.lf-bottom-margin[data-bind*='activiteitenOptions']")))).select_by_visible_text('Pilates')
  • 使用 * XPATH *:
driver.get("https://prd.mylogifit.com/logifitweb/genericwidgetbyid.html?a=Touchee&widgetId=73f1f696-5063-4fb7-9a8d-c588ef149eb4")
Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='lf-simpelselect lf-bottom-margin' and contains(@data-bind, 'activiteitenOptions')]")))).select_by_visible_text('Pilates')
      • 注意**:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
  • 浏览器快照:

参考文献

您可以在以下位置找到一些相关讨论:

  • 如何使用Selenium和Python从下拉菜单中选择选项
nbewdwxp

nbewdwxp2#

from selenium.webdriver.support.ui import Select

lesson_droplist = Select(driver.find_element(By.CLASS_NAME, "lf-simpelselect"))
lesson_droplist.select_by_visible_text("Afro")

您应该按可见文本*而不是 * 值进行选择
如果要按值选择,则

lesson_droplist= Select(driver.find_element(By.CLASS_NAME, "lf-simpelselect"))
lesson_droplist.select_by_value("505e1c62-9e68-4569-a7ef-b6566ff1b56d") #Afro's value
e3bfsja2

e3bfsja23#

类名有多个匹配项,这就是您收到该错误的原因,请尝试以下XPath:

from selenium.webdriver.support.select import Select

dropdown = Select(driver.find_element(By.XPATH, "(.//select[@class='lf-simpelselect lf-bottom-margin'])[2]"))
dropdown.select_by_visible_text("Afro")
eqoofvh9

eqoofvh94#

您可以使用XPath来解决等待问题,有两个选项:时间库或WebDriverWait。
你可以在时间图书馆试试这个:

import time

time.sleep(3)
dropdown = Select(browser.find_element(By.XPATH, "(.//select[@class='lf-simpelselect lf-bottom-margin'])[2]"))
dropdown.select_by_visible_text("Afro")
time.sleep(3)
text = browser.find_element(By.XPATH, "//div[@data-bind='text: lesOmschrijving()']").text
print(text)

您可以在WebDriverWait中尝试此操作(但它只是等待此XPath,您可能需要另一次等待以获取另一个文本):

wait.until(EC.presence_of_element_located((By.XPATH, "(.//select[@class='lf-simpelselect lf-bottom-margin'])[2]")))

通常,您需要等待以确保相关元素加载到浏览器中。

相关问题