获得所有 <li> 的 <ul> 硒 python3
get all <li> of <ul> selenium python3
我有一个 html 这样的:
<ul class="page">
<li id="p1"></li>
<li id="p2"></li>
<li id="p3"></li>
</ul>
我尝试使用以下代码在 python 中使用 selenium Webdriver
找到
下的所有 - :
my_ul = driver.find_element_by_xpath("//ul[@class='page']")
all_li = my_ul.find_element_by_tag_name("li")
for li in all_li:
print(li.text)
但是当我运行它时出现错误:
> for li in all_li:
E TypeError: 'WebElement' object is not iterable
谁能给我解释一下这是怎么回事??
非常感谢!
find_element_by_tag_name
returns 单个元素,您需要获取列表以对其进行迭代。为此,您需要使用 find_elements_by_tag_name
.
你的代码应该是这样的:
my_ul = driver.find_element_by_xpath("//ul[@class='page']")
all_li = my_ul.find_elements_by_tag_name("li")
for li in all_li:
print(li.text)
我有一个 html 这样的:
<ul class="page">
<li id="p1"></li>
<li id="p2"></li>
<li id="p3"></li>
</ul>
我尝试使用以下代码在 python 中使用 selenium Webdriver
找到
- 下的所有
- :
my_ul = driver.find_element_by_xpath("//ul[@class='page']") all_li = my_ul.find_element_by_tag_name("li") for li in all_li: print(li.text)
但是当我运行它时出现错误:
> for li in all_li: E TypeError: 'WebElement' object is not iterable
谁能给我解释一下这是怎么回事??
非常感谢!
find_element_by_tag_name
returns 单个元素,您需要获取列表以对其进行迭代。为此,您需要使用 find_elements_by_tag_name
.
你的代码应该是这样的:
my_ul = driver.find_element_by_xpath("//ul[@class='page']")
all_li = my_ul.find_elements_by_tag_name("li")
for li in all_li:
print(li.text)