如何使用Selenium、Python选择下拉菜单值

2024-04-25 16:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要,单击下拉菜单中的menü元素。但我是新的Selenium网络驱动程序,我什么都找不到

<div class="sort-fltr-cntnr"><select>
<option value="SCORE">Önerilen</option>
<option value="PRICE_BY_ASC">En Düşük Fiyat</option>
<option value="PRICE_BY_DESC">En Yüksek Fiyat</option>
<option value="MOST_RECENT">En Yeniler</option>
<option value="BEST_SELLER">Çok Satanlar</option> ....

这是我的代码:

dropdown = self.driver.find_element(By.CSS_SELECTOR, "select")
element=dropdown.find_element(By.XPATH, "//option[. = 'Önerilen']")
self.driver.execute_script("arguments[0].click();", element)
time.sleep()
dropdown.find_element(By.XPATH, "//option[. = 'En Düşük Fiyat']").click()

给定的错误:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //option[. = 'Önerilen']

1条回答
网友
1楼 · 发布于 2024-04-25 16:32:17
from selenium import webdriver
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

select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "sort-fltr-cntnr"))))
select.select_by_value('SCORE')

只需等待可单击带有排序的类名,然后选择分数值

相关问题 更多 >