I'm trying to retrieve the correct text inside an element. Here is the output:
(Pdb) p etree.tostring(els[0])
'<h5 class="msg-delivered" style="padding:0;text-rendering:optimizeLegibility;line-height:1.1;margin-bottom:15px;-webkit-font-smoothing:antialiased;font-family:"Open Sans", "Helvetica Neue", Arial, Helvetica, sans-serif;color:#888888;vertical-align:middle;margin:0;font-size:13px;font-weight:300 !important"> \n<i class="ic-icon-delivered" style="margin:0;padding:0;font-family:"Open Sans", "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;text-rendering:optimizeLegibility;position:relative;background:url(https://d1s8987jlndkbs.cloudfront.net/assets/sprite-ratings-ee0696744f54df6536179c70e24217e3.png) no-repeat -12px -12px;background-size:132px 436px;display:none;vertical-align:middle;width:25px;height:25px;background-position:-16px -16px;top:0"/> \nYour order was delivered \non \n6/4 \n@ \n4:44 PM \n</h5> \n'
(Pdb) p els[0].text
'\r\n'
How can I get the string: "Your item was delivered on 6/4 at 4:40 PM"? I can use a regex on the etree.tostring() output, but wanted to know why the els[0].text option was not working?
You can try using xpath function
string()
which return concatenated value of all text nodes within current element :output :