[英]How to click on the list of the <a> elements in an <ul> and <li> elements with selenium using python?
[英]How to get a list of the <li> elements in an <ul> with Selenium using Python?
我正在使用使用 Python 的 Selenium WebDriver 进行 UI 测试,我想检查以下 HTML:
<ul id="myId">
<li>Something here</li>
<li>And here</li>
<li>Even more here</li>
</ul>
从这个无序列表中,我想遍历元素并检查其中的文本。 我通过其id
选择了 ul 元素,但我找不到任何方法来遍历 Selenium 中的<li>
-children。
有谁知道如何使用 Selenium(在 Python 中)遍历无序列表的<li>
-childeren?
您需要使用.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
奇怪的是,我不得不使用这个 get_attribute()-workaround 来查看内容:
html_list = driver.find_element_by_id("myId")
items = html_list.find_elements_by_tag_name("li")
for item in items:
print(item.get_attribute("innerHTML"))
您可以使用列表理解:
# Get text from all elements
text_contents = [el.text for el in driver.find_elements_by_xpath("//ul[@id='myId']/li")]
# Print text
for text in text_contents:
print(text)
我正在使用使用 Python 的 Selenium WebDriver 进行 UI 测试,我想检查以下 HTML:
<ul id="myId">
<li>Something here</li>
<li>And here</li>
<li>Even more here</li>
</ul>
从这个无序列表中,我想遍历元素并检查其中的文本。 我通过id
选择了 ul 元素,但我找不到任何方法来遍历 Selenium 中的<li>
-children。
有人知道如何使用 Selenium(在 Python 中)遍历无序列表的<li>
-childeren 吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.