1

I use soup = BeautifulSoup(driver.page_source) to parse the whole page from Selenium in BeautifulSoup.

But how to just parse one element of Selenium in BeautifulSoup.

Below code will throw

TypeError: object of type 'FirefoxWebElement' has no len()

element = driver.find_element_by_id(id_name) soup = BeautifulSoup(element) 
6
  • try BeautifulSoup(element.text, 'lxml'), lxml is the parser Commented Oct 30, 2019 at 2:15
  • @AhmedI.Elsayed element.text will just return the text, and lost all the tag. Commented Oct 30, 2019 at 2:23
  • You want to get an element say like <div> Hi </div> , right? Commented Oct 30, 2019 at 2:41
  • Yes, It may have many sub tag in it. Commented Oct 30, 2019 at 2:42
  • I answered, have a look Commented Oct 30, 2019 at 2:47

1 Answer 1

2

I don't know if selenium does this out of the box, but I managed to find this workaround

element_html = f"<{element.tag_name}>{element.get_attribute('innerHTML')}</{element.tag_name}>" 

you may want to replace innerHTML with innerTEXT if you want to get only the text, for example

<li>Hi <span> man </span> </li> 

Getting the innerHTML will return all of what inside but the innerTEXT won't, try & see.

now create your Soup object

soup = BeautifulSoup(element_html) print(soup.WHATEVER) 

using the above technique, just create a method parseElement(webElement) & use it whenever you want to parse an element.

Btw I only use lxml & when I forgot to type it, the script didn't work

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.