I'm using Selenium WebDriver using Python for UI tests and I want to check the following HTML:
我正在使用Selenium WebDriver使用Python进行UI测试,我想查看以下HTML:
<ul id="myId">
<li>Something here</li>
<li>And here</li>
<li>Even more here</li>
</ul>
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.
从这个无序列表中,我想循环遍历元素并检查其中的文本。我通过它的id选择了ul元素,但我找不到任何方法来遍历Selenium中的
Does anybody know how you can loop over the <li>
-childeren of an unordered list with Selenium (in Python)?
有没有人知道如何使用Selenium(在Python中)循环无序列表的
2 个解决方案
#1
14
You need to use the .find_elements_by_
method.
您需要使用.find_elements_by_方法。
For example,
例如,
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
#2
0
You can use list comprehension:
您可以使用列表理解:
# 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
#1
14
You need to use the .find_elements_by_
method.
您需要使用.find_elements_by_方法。
For example,
例如,
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
#2
0
You can use list comprehension:
您可以使用列表理解:
# 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