【发布时间】:2020-06-29 18:35:27
【问题描述】:
要遍历下拉列表,我想列出下拉列表中的所有元素,以便让我的代码一一选择每个元素。
不幸的是,下拉菜单不是选择元素,(如果我没记错的话)所以我不能使用类似的东西:
options = [x for x in select_box.find_elements_by_tag_name("option")]
我尝试通过首先打开下拉菜单来做到这一点
Bedrijfsindeling_dropdown = driver.find_element_by_xpath('//span[@id="ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_ctl01"]')
Bedrijfsindeling_dropdown.click()
time.sleep(1)
然后在打开下拉菜单后,我想在下拉菜单中找到所有元素
我尝试通过使用 xpath 来做到这一点
Parent = driver.find_element_by_id("ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_divDropDown")
all_children_by_xpath = Parent.find_elements_by_xpath(".//*")
print(all_children_by_xpath)
print('len(all_children_by_xpath): ' + str(len(all_children_by_xpath)))
但是,看起来我只能找到 6 个元素。下拉列表包括(我猜,我没有计算它们。但超过 6 个)大约 50 个不同的选项。
有人知道我如何找到所有不同的元素吗?另外,循环遍历每个元素的最快和最简单的方法是什么?
使用的链接:https://www.arbeidsmarktcijfers.nl/Report/4
下拉我想找到元素:“Bedrijfsindeling”
【问题讨论】: