[英]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)
You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.