如何提取所有< li>< ul>下的元素 [英] How to extract all <li> elements under <ul>

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

问题描述

我想提取我尝试过的< ul> 下的所有< li> 元素文本

  elem = driver.find_elements_by_xpath(("//div [@ class ='left width50']/p/b/ul"))len(elem) 

给出"0"或空白列表.

这是html来源

 < div class ="left width50">< p>< b>特征:</b>/p>< ul>< li>容易喷雾施用</li>< li>优异的粘合性能</li>< li>单包</li>< li>在工作现场与干净的饮用水混合</li></ul></div> 

HERE是网站的

I want to extract all <li> element text that are under <ul> for which I tried

elem = driver.find_elements_by_xpath(("//div[@class='left width50']/p/b/ul"))
len(elem)

gives '0' or empty list.

here is the html source

<div class="left width50">
                            <p><b>Features:</b></p>
                            <ul>
                                    <li>Easy spray application</li>
                                    <li>Excellent bonding properties</li>
                                    <li>Single package</li>
                                    <li>Mixed with clean potable water at job site</li>
                            </ul>
                        </div>

HERE is the link of the website

How to go about it any suggestions?

解决方案

Actually you're trying to find the path after the p and b tag. that will look something like this.

<div class="left width50">
    <p><b>Features:<ul>
            <li>Easy spray application</li>
            <li>Excellent bonding properties</li>
            <li>Single package</li>
            <li>Mixed with clean potable water at job site</li>
    </ul></b></p>

</div>

But your code is different in HTML.

So you should look around without the p and b tag.

Here is the quick help you can take from chrome. Go to developer option using f12 key and navigate to elements tab and then right click on the element which you want to find out and select the selector value.

You can read more about what are the ways to find the element here

If you want to use the xPath this is right xpath for you - //*[@id="borderForGrid"]/div[1]/ul

Extraction Process

Once you'll get all the ul this will help you to get all the li text

all_li = all_ul_from_xpath.find_elements_by_tag_name("li")
for li in all_li:
    text = li.text
    print (text)

Working code for reference.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("http://www.carboline.com/products/")



elem = driver.find_element_by_xpath('//*[@id="borderForGrid"]/div[1]/ul')

all_li = elem.find_elements_by_tag_name("li")
for li in all_li:
    text = li.text
    print (text)

Output

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

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