How to get ID of target by name?

Hello.

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

try:
  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?

Hi.

Here is output

Traceback (most recent call last):
File “./modify_target.py”, 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

TechDoc Portal

Could you try

output = target.get('id')

instead?

1 Like

Yes,

output = target.get('id')

Works fine for me.
Thanks a lot.