Home > Back-end >  Print html tags inside selected tag in python using selenium
Print html tags inside selected tag in python using selenium

Time:02-10

Suppose this is the dummy html code that I am accessing:

<ul >
          <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>

CodePudding user response:

Use BeautifulSoup

from bs4 import BeautifulSoup

# sample html
html = """<ul >
          <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>]
  • Related