selenium-webdriverxpathselenium-ide

Selenium IDE how to extract the id of an attribute with xpath


I am trying to get the id and store it as a variable with the following/

Command Target Value
store value xpath=(//div[@class[starts-with(.,'commentItem')]]/@id)[1] Id

but I got this error message:

storeValue on xpath=(//div[@class[starts-with(.,'commentItem')]]/@id)[1] with value Id Failed: 11:54:55 The result of the xpath expression "(//div[@class[starts-with(.,'commentItem')]]/@id)[1]" is: [object Attr]. It should be an element.

I have tried this xpath expression in the console and it works, but impossible to get this id here


Solution

  • The issue you're facing is due to the fact that the storeValue command in Selenium IDE is designed to extract the value of an attribute from an HTML element, not the attribute itself.

    In your case, you're trying to extract the id attribute from a div element. The XPath expression you're using is actually returning the id attribute itself, not an HTML element, which is why you're seeing the error message.

    To fix this, you need to use the storeAttribute command instead of storeValue. The storeAttribute command is designed to extract the value of an attribute from an HTML element.

    Here's how you can modify your command:

    Command : storeAttribute

    Target : xpath=(//div[starts-with(@class, 'commentItem')]/@id)[1]

    Value : Id

    This command will store the value of the id attribute of the first div element with a class that starts with commentItem in the variable Id.