I've been following the examples from HERE and HERE trying parse a SOAP response, but not able to get the element I want.
Example SOAP Response:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns="urn:partner.soap.sforce.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<loginResponse>
<result>
<metadataServerUrl>meta</metadataServerUrl>
<passwordExpired>false</passwordExpired>
<sandbox>true</sandbox>
<serverUrl>someUrl</serverUrl>
<sessionId>sessionId###</sessionId>
<userId>userId###</userId>
<userInfo></userId>
</result>
</loginResponse>
</soapenv:Body>
</soapenv:Envelope>
Trying to get the sessionId
but getting None
or empty list back.
Example Code:
import xml.etree.ElementTree as ET
...
r = requests.post(url, headers=header, data=payload)
data = r.content
ns = {
"soapenv": "http://schemas.xmlsoap.org/soap/envelope/"
}
root = ET.fromstring(data)
sid = root.findall(".//soapenv:sessionId", ns)
# Tried these and any combination of those
#sid = root.findall("./soapenv:Body/soapenv:loginResponse/soapenv:result/soapenv:sessionId", ns)
#sid = root.findall("./soapenv:Body/loginResponse/result/sessionId", ns)
#sid = root.findall("soapenv:sessionId", ns)
print(sid)
Can anyone help with this?