如何获取< li>的列表< ul>中的元素使用Python使用Selenium? [英] How to get a list of the <li> elements in an <ul> with Selenium using Python?

查看:266
本文介绍了如何获取< li>的列表< ul>中的元素使用Python使用Selenium?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium WebDriver和Python进行UI测试,我想检查以下HTML:

I'm using Selenium WebDriver using Python for UI tests and I want to check the following HTML:

<ul id="myId">
    <li>Something here</li>
    <li>And here</li>
    <li>Even more here</li>
</ul>

从这个无序列表中,我想遍历元素并检查其中的文本.我通过其id选择了ul-element,但找不到任何方法可以循环使用Selenium中的<li> -children.

From this unordered list I want to loop over the elements and check the text in them. I selected the ul-element by its id, but I can't find any way to loop over the <li>-children in Selenium.

有人知道如何用Selenium(在Python中)遍历无序列表的<li> -childeren吗?

Does anybody know how you can loop over the <li>-childeren of an unordered list with Selenium (in Python)?

推荐答案

您需要使用.find_elements_by_方法.

例如,

html_list = self.driver.find_element_by_id("myId")
items = html_list.find_elements_by_tag_name("li")
for item in items:
    text = item.text
    print text

这篇关于如何获取&lt; li&gt;的列表&lt; ul&gt;中的元素使用Python使用Selenium?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆