Reputation: 23
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
Upvotes: 1
Views: 567
Reputation: 111
As Shadique correctly described, the storeAttribute command is the one to use.
However, there is a special feature to be noted when using it in Selenium IDE. Selenium IDE interprets the XPath expression passed in to determine the two components "XPath to the HTML element" and "Name of the attribute" and then checks these for validity. Selenium IDE probably only uses the "@" character and not the character string "/@" to identify the separation of the two components. The backslash is therefore added to the XPath of the HTML element and this results in an invalid (incomplete) XPath expression.
Just leave out the backslash before the "@" sign in the XPath argument of the "storeAttribute" command and it should work:
//div[starts-with(@class, 'commentItem')]@id
When using it in my scripts, I unfortunately discovered the hard way that the "storeAttribute" command was not implemented consistently when you include the use of variables. Only the XPath part of the HTML element is interpreted, unfortunately not the name of the attribute.
Example 1: --> doesn't work!
Command: store
Target: //div[starts-with(@class, 'commentItem')]
Value: xpathElement
Command: store
Target: id
Value: nameAttribute
Command: storeAttribute
Target: xpath=${xpathElement}@${nameAttribute}
Value: currentValue
Example 2: --> works
Command: store
Target: //div[starts-with(@class, 'commentItem')]
Value: xpathElement
Command: storeAttribute
Target: xpath=${xpathElement}@id
Value: currentValue
I guess using an index on the entire XPath expression "(...)[1]" is impossible because of Selenium IDE's naive splitting of the entire XPath expression based on the "@" character.
Maybe it would work like this, but I haven't tested it.
xpath=(//div[starts-with(@class, 'commentItem')])[1]@id
Upvotes: 0
Reputation: 64
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.
Upvotes: 0