简体   繁体   English

如何使用带有Python的Selenium Webdriver从ul li下拉列表中获取所有选项?

[英]How to get all options from ul li drop-down using Selenium Webdriver with Python?

The project that I am working on changes drop-down lists from simple ones to ul li. 我正在处理的项目将下拉列表从简单列表更改为ul li。 Prior to this to scrap all options from a drop down list I would do the following: 在此之前,从下拉列表中删除所有选项,我将执行以下操作:

myList = [] 
el = browser.find_element_by_id('Filter_ClientDistrict')
for option in el.find_elements_by_tag_name('option'):
    myList.append(option.get_attribute("value"))

How can scrap all the options from the same drop down list if its code looks like this now: 如果现在代码看起来像这样,如何从相同的下拉列表中删除所有选项:

在此处输入图片说明

It should be 它应该是

browser.find_element_by_css_selector('#Filter_ClientDistrict button.dropdown-toggle').click()
WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#Filter_ClientDistrict ul li"))
    myList = [] 
    el = browser.find_element_by_id('Filter_ClientDistrict')
    for option in el.find_elements_by_css_selector('ul li'):
        myList.append(option.get_attribute("innerText").strip())

Selenium python:获取所有<li>所有的文本<ul>从一个<div></div><div id="text_translate"><p>我想从几页中获取所有作为dutch word = english word的单词列表。</p><p> 通过检查 HTML,这意味着我需要从#mw-content-text的子 div 中获取所有ul的所有li中的所有文本。</p><p> 这是我的代码:</p><pre> from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('headless') # start chrome without opening window driver = webdriver.Chrome(chrome_options=options) listURL = [ "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Basics_1", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Basics_2", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Phrases_1", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Negative_1", ] list_text = [] for url in listURL: driver.get(url) elem = driver.find_elements_by_xpath('//*[@id="mw-content-text"]/div/ul') for each_ul in elem: all_li = each_ul.find_elements_by_tag_name("li") for li in all_li: list_text.append(li.text) print(list_text)</pre><p> 这是 output</p><pre> ['man = man', 'vrouw = woman', 'jongen = boy', 'ik = I', 'ben = am', 'een = a/an', 'en = and', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']</pre><p> 我不明白为什么有些li文本即使它们的 xpath 相同也无法检索(我通过开发人员控制台的副本 xpath 仔细检查了其中的几个)</p></div></ul></li> - Selenium python: get all the <li> text of all the <ul> from a <div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有 Python 的 Selenium Webdriver 抓取下拉菜单选项? - How to scrape drop-down menu options using Selenium Webdriver with Python? 如何使用 select 这个非 select 下拉菜单中的元素使用 Selenium Webdriver 和 ZA7F57F35426B9387411 - How to select this element from this non select drop-down menu using Selenium Webdriver and Python 使用Selenium Webdriver从python的下拉列表中选择选项 - Selecting options from drop down list in python using Selenium Webdriver 如何使用带有 Python 的 Selenium Webdriver 使用 mat-options 找到下拉列表的长度? - How to find length of drop-down with mat-options with Selenium Webdriver with Python? 无法使用Selenium Web驱动程序python从自动完成下拉列表中选择多个选项 - Unable to select multiple options from autocomplete drop-down using selenium web-driver python 使用 Selenium Python 从包含多个下拉选项的动态表中抓取数据 - Scraping data from a dynamic table containing multiple drop-down options using Selenium Python Selenium python:获取所有<li>所有的文本<ul>从一个<div></div><div id="text_translate"><p>我想从几页中获取所有作为dutch word = english word的单词列表。</p><p> 通过检查 HTML,这意味着我需要从#mw-content-text的子 div 中获取所有ul的所有li中的所有文本。</p><p> 这是我的代码:</p><pre> from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('headless') # start chrome without opening window driver = webdriver.Chrome(chrome_options=options) listURL = [ "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Basics_1", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Basics_2", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Phrases_1", "https://duolingo.fandom.com/wiki/Dutch_(NL)_Skill:Negative_1", ] list_text = [] for url in listURL: driver.get(url) elem = driver.find_elements_by_xpath('//*[@id="mw-content-text"]/div/ul') for each_ul in elem: all_li = each_ul.find_elements_by_tag_name("li") for li in all_li: list_text.append(li.text) print(list_text)</pre><p> 这是 output</p><pre> ['man = man', 'vrouw = woman', 'jongen = boy', 'ik = I', 'ben = am', 'een = a/an', 'en = and', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']</pre><p> 我不明白为什么有些li文本即使它们的 xpath 相同也无法检索(我通过开发人员控制台的副本 xpath 仔细检查了其中的几个)</p></div></ul></li> - Selenium python: get all the <li> text of all the <ul> from a <div> 如何在Python中获取Selenium以使用下拉式日历? - How do to get Selenium in Python to use a drop-down calender? 使用selenium python从下拉选项中选择一个值 - Selecting a value from a drop-down option using selenium python 如何使用 Python 与 Selenium 一起 select 下拉菜单值? - How to select a drop-down menu value with Selenium using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM