Use Google Assistant's Driving mode

Use Google Assistant's Driving mode

Use Google Assistant's Driving mode

Non empty h2 element but lxml text is empty

48 views Asked by At

I'm trying to parse the below html

<div class="no-margin">
  <h2 data-outlined="false" class="">Use Google Assistant's Driving mode</h2>

  <p ...

taken from https://support.google.com/assistant/answer/6041199. I have the lxml element for the parent div with class no-margin and I iterate its getchildren(). The first child element is the one with h2 but its text returns None and tail returns \n\n. Why is the text None? etree.tostring for the h2 element prints b'<h2/>\n\n' if it is any relevant.

Any help is much appreciated.

1

There are 1 answers

0
Martin Honnen On

I tried a minimal example parsing that fragment with e.g.

for child in html.fragment_fromstring('''<div class="no-margin"><h2>Use Google Assistant's Driving mode</h2>
<p>Assistant driving mode helps you complete actions while you drive. With your voice, you can read and send messages, make calls, and control media.</p
>
<p><strong>Important:</strong> Some features aren't available in all languages, countries, or eligible Android devices.</p></div>'''):
    print(child.text)

Result:

Use Google Assistant's Driving mode
Assistant driving mode helps you complete actions while you drive. With your voice, you can read and send messages, make calls, and control media.
None

Consider to edit your question and to provide a minimal but complete sample allowing others to reproduce the problem.