Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lxml text retrieval (find_txt) shouldn't fail if no text element available #1242

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions napalm/base/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def textfsm_extractor(cls, template_name, raw_text):
def find_txt(xml_tree, path, default="", namespaces=None):
"""
Extracts the text value from an XML tree, using XPath.
In case of error, will return a default value.
In case of error or text element unavailability, will return a default value.

:param xml_tree: the XML Tree object. Assumed is <type 'lxml.etree._Element'>.
:param path: XPath to be applied, in order to extract the desired data.
Expand All @@ -265,7 +265,10 @@ def find_txt(xml_tree, path, default="", namespaces=None):
if xpath_length and xpath_applied[0] is not None:
xpath_result = xpath_applied[0]
if isinstance(xpath_result, type(xml_tree)):
value = xpath_result.text.strip()
if xpath_result.text:
value = xpath_result.text.strip()
else:
value = default
else:
value = xpath_result
else:
Expand Down