【问题标题】:find and list all elements in dropdown selenium python查找并列出下拉 selenium python 中的所有元素
【发布时间】: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”

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    我找到了做我想做的事的方法。

    Bedrijfsindeling_dropdown = driver.find_element_by_xpath('//span[@id="ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_ctl01"]')
    Bedrijfsindeling_dropdown.click()
    time.sleep(1)
    Parent = driver.find_element_by_id("ctl00_ContentPlaceHolder1_ReportViewer1_ctl09_ctl21_divDropDown")
    all_children_by_xpath = Parent.find_elements_by_xpath(".//input[@id]")
    
    for ids in all_children_by_xpath:
            print(ids.get_attribute('id'))
    time.sleep(1)
    Bedrijfsindeling_dropdown.click()
    

    如果有人知道更好的方法或知道一种轻松遍历所有 id 的方法,我很想听听您的解决方案

    【讨论】:

    • 您可以接受自己的答案,以便将来人们可以参考。
    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2013-11-10
    • 2021-04-28
    相关资源
    最近更新 更多