How to get ID of target by name?


I try to get ID of target object by given name via gmp class like this:

  targets = []

  with gmp:
    gmp.authenticate(username, password)

    targets = gmp.get_targets(filter='name=my-target-name')
    for target in targets.xpath('target'):
      output = target.find('id').text

But i have an error with last string.
Could you tel me how i can get id by name correctly ?

The code looks fine at first sight. Could you paste the error traceback?


Here is output

Traceback (most recent call last):
File “./”, line 40, in
output = target.find(‘id’).text
AttributeError: ‘NoneType’ object has no attribute ‘text’

Therefore it doesn’t find “id” data ?
But if a search “name” it works okay

You are trying to access an xml attribute as child element. Target id is an xml attribute. See

Could you try

output = target.get('id')


1 Like


output = target.get('id')

Works fine for me.
Thanks a lot.