Issue
Suppose this is the dummy html code that I am accessing:
<ul class="some class">
<li id ="a">Arranged in any order</li>
<li id ="b" >Will still make sense</li>
</ul>
I am running a python script to automate a certain website and I need to extract all li
tags from certain ul
tag. I have extracted the ul
tag using the xPath. When I try to print the ul
using ul.text
.
It prints:
Arranged in any order
Will still make sense
However I want the li
tags along with the attributes like id
. Is there any way in selenium to do this thing.
Desired output:
<li id ="a">Arranged in any order</li>
<li id ="b" >Will still make sense</li>
Solution
Use BeautifulSoup
from bs4 import BeautifulSoup
# sample html
html = """<ul class="some class">
<li id ="a">Arranged in any order</li>
<li id ="b" >Will still make sense</li>
</ul>"""
# create the soup from the html
# when using selenium, replace html with driver.page_source
# soup = BeautifulSoup(driver.page_source, 'html.parser')
soup = BeautifulSoup(html, 'html.parser')
# find the ul tag where class is "some class" and then find all the li tags
lst_of_li = soup.find('ul', class_='some class').findAll('li')
print(lst_of_li)
# [<li id="a">Arranged in any order</li>, <li id="b">Will still make sense</li>]
Answered By - It_is_Chris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.