简体   繁体   English

Python selenium 将 li 添加到 ul

[英]Python selenium add li to ul

I have this html code我有这个 html 代码

<ul class="select-items">
     <li class="select-items-item" data-label="<first>">Number 1</li>
</ul>

and I want to add a new li like <li class="select-items-item" data-label="<second>">Number 2</li>我想添加一个新的 li 像<li class="select-items-item" data-label="<second>">Number 2</li>

What i tried so far: I click the list using the xpath ( that works fine because I can see the list open), addAttribute to the element (that is the tricky part, not working), add javascript code ( not working either)到目前为止我尝试了什么:我使用 xpath 单击列表(因为我可以看到列表打开,所以工作正常),将属性添加到元素(这是棘手的部分,不起作用),添加 javascript 代码(也不起作用)

liItem = """
     var ul = document.getElementByXpath('//*[@class="select-items"]');
     var li = document.createElement("li");
     li.appendChild(document.createTextNode("Element 4"));
     ul.appendChild(li);
 """
 driver.execute_script(liItem);

Any suggestions?有什么建议么?

To add the following <li> ie添加以下<li>

<li class="select-items-item" data-label="<second>">Number 2</li>

You can use the following line of code:您可以使用以下代码行:

scriptTxt = """
    var ul = document.getElementsByClassName('select-items').item(0);
    var li = document.createElement('li');
    li.setAttribute('textContent', 'Number 2');
    ul.appendChild(li);body.appendChild(li);
"""
driver.execute_script(scriptTxt)

References参考

You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:

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.

相关问题 Selenium 自动化使用 Python 和 Selenium 在 ul 中选择 li 菜单项 - Selenium Automation to Select a li Menu Item in An ul with Python and Selenium 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> 如何获得列表<li>中的元素<ul>使用 Python 使用 Selenium?</ul></li> - How to get a list of the <li> elements in an <ul> with Selenium using Python? 如何在 python 中使用 Selenium 在 ul 中单击 li? - How do I click an li inside a ul using Selenium in python? 如何点击<a>元素</a>列表<ul><a>和</a><li><a>元素与硒使用python?</a> - How to click on the list of the <a> elements in an <ul> and <li> elements with selenium using python? 如何点击列表中的<li>中的元素<ul> python中含有硒的元素? - How to click on the list of the <li> elements in an <ul> elements with selenium in python? python正则表达式替换 <ul…> 和 <li…> 标签与 <ul> 和 <li> ,分别 - python regex replace <ul…> and <li…> tags with <ul> and <li>, resp 我怎样才能得到第10项<li>中的元素<ul>使用 Selenium 使用 Python?</ul></li> - How can I get the 10th item of the <li> elements in an <ul> with Selenium using Python? Selenium: Select 并直接迭代<li> a的子元素<ul>父母使用 python?</ul></li> - Selenium: Select and iterate through direct <li> child elements of a <ul> parent using python? Python Selenium:get_elements 方法没有获取 ul 中的 li 项 - Python Selenium: get_elements method does not get li items in ul
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM