Trying to select multiple elements from dropdown menu via selenium in python.
Website from URL. But Timeoutexception error is occurring.

我试过谷歌的判断菜单.//label[@for="inputGenre"]/parent::div//select[@placeholder="Choose a Category"]正好提供了我需要的SELECT标记.但不幸的是,使用Selify我在这个标签中找不到任何元素.你知道为什么会出现这个错误吗?

代码如下;

slect_element = Select(WebDriverWait(driver, 10).until(EC.element_located_to_be_selected((By.XPATH, '//label[@for="inputGenre"]/parent::div//select[@placeholder="Choose a Category"]'))))

slect_element.select_by_index(1)
slect_element.select_by_value('23')

奇怪的是,可以从下面的代码中找到它并获得它的文本值;

drp_menu=driver.find_elements(By.XPATH,'//label[@for="inputGenre"]/parent::div//div[@class="dropdown-main"]/ul/li')
print(len(drp_menu))
ls_categories=[]
for i in drp_menu:
    ls_categories.append(i.get_attribute('innerText'))

Print提供了15个元素,而Get_Attribute(InnerText)提供了每个选项元素的文本. 无论如何,非常感谢"先知"

推荐答案

That Select element is hidden and can't be used by Selenium as we use normal Select elements to select drop-down menu items.
Here we need to open the drop-down as we open any other elements by clicking them, select the desired options and click the Search button.
So, these lines are opening that drop down and selecting 2 options in the drop-down menu:

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-group'][contains(.,'Category')]//div[@class='dropdown-display-label']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-group'][contains(.,'Category')]//li[@data-value='23']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-group'][contains(.,'Category')]//li[@data-value='1']"))).click()

到目前为止,结果是:

enter image description here

最后,通过单击搜索按钮,结果是:

enter image description here

整个代码是:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
wait = WebDriverWait(driver, 20)

url = "https://channelcrawler.com/"

driver.get(url)

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-group'][contains(.,'Category')]//div[@class='dropdown-display-label']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-group'][contains(.,'Category')]//li[@data-value='23']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-group'][contains(.,'Category')]//li[@data-value='1']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()

Python相关问答推荐

ConversationalRetrivalChain引发键错误

从列表中获取n个元素,其中list [i][0]== value''

Gekko中基于时间的间隔约束

为什么我的sundaram筛这么低效

以异步方式填充Pandas 数据帧

如何在Gekko中处理跨矢量优化

在round函数中使用列值

为什么我的scipy.optimize.minimize(method=";newton-cg";)函数停留在局部最大值上?

无法使用请求模块从网页上抓取一些产品的名称

如何在不不断遇到ChromeDriver版本错误的情况下使用Selify?

使用元组扩展字典的产品挑战

Django REST框架+Django Channel->;[Errno 111]连接调用失败(';127.0.0.1';,6379)

如何判断变量可调用函数的参数是否都属于某个子类?

给定Pandas 列DataFrame中的一个值,在其他DataFrame中 Select 与该值最接近的N行

两极中的`df.Query()`?

带参数约束的类型提示函数*args->;tuple[*args]

在Pandas 数据帧中使用GROUPBY()应用FIND_PEAKS()函数

有没有办法一次删除字典里的几个条目?

CKEditor更新通知

Shopware 6 REST-API产品更新不起作用