XPath Find full HTML element ID from partial ID

22,038

Solution 1

Thanks to Thomas Jung I have been able to figure this out. If I use:

//*[contains(./@id, 'f41_txt')]/@id

This will return just the ID I am looking for.

Solution 2

You can use contains to select the element:

//*[contains(@id, 'f41_txt')]
Share:
22,038
user228178
Author by

user228178

Updated on September 21, 2020

Comments

  • user228178
    user228178 almost 4 years

    I am looking to write an XPath query to return the full element ID from a partial ID that I have constructed. Does anyone know how I could do this? From the following HTML (I have cut this down to remove work specific content) I am looking to extract f41_txtResponse from putting f41_txt into my query.

    <input id="f41_txtResponse" class="GTTextField BGLQSTextField2 txtResponse"  value="asdasdadfgasdfg" name="f41_txtResponse" title="" tabindex="21"/>
    

    Cheers

  • user228178
    user228178 over 14 years
    With the way we are generating the webpage this is retrieving everything where I am just looking for the element ID.
  • Thomas Jung
    Thomas Jung over 14 years
    //*[contains(@id, 'f41_txt')] will select a list of tags you have to iterate over it and select the id attribute for each of them. You can use count(//*[contains(@id, 'f41_txt')]) to get the total number.
  • user228178
    user228178 over 14 years
    Thats not the best way to do this. I have answered my own question though with the help of you answer.
  • Tomalak
    Tomalak over 14 years
    Be aware that this can fail. You might want to use //*[starts-with(@id, 'f41_txt')]/@id instead.
  • Admin
    Admin over 14 years
    praise the lord, thanks to thomas jung i remembered that you always need the @ sign to find atrributes. duh.