views:

415

answers:

3

I have the following code in my script:

System.out.println(selenium.getAttribute("xpath=//div[@class='guest clearfix'][1]/@id"));

When I try to run the script, it says the element is not found. If I enter the xpath into XPather (addon for firefox) //div[@class='guest clearfix'][1]/@id, it will correctly give me the id.

I am stumped as to why it will not run in my code. If anyone can see any error in my code, please let me know.

Thanks

+2  A: 

I think the problem will be the extra forward slash before the @id. Try the following:

selenium.getAttribute("//div[@class='guest clearfix'][1]@id");

I believe the final /@id is valid XPath for returning an attribute, but Selenium's syntax is different as it requires an element locator followed by an @ and attribute name.

Additionally, you could achieve the same with the following CSS locator:

selenium.getAttribute("css=div.guest.clearfix@id");
Dave Hunt
Hi Dave, I tried both suggestions that you posted, and both are still not finding the element. The error I am recieving is:com.thoughtworks.selenium.SeleniumException: ERROR: Element //div[@class='guest clearfix'][1] not foundand when I try CSS, I get:com.thoughtworks.selenium.SeleniumException: ERROR: Element css=div.guest.clearfix[1] not found
TesterGuy
It'd be worth updating your question with a snippet of the HTML. Is it possible that the element has loaded when the command runs? If so, it might be worth adding a `waitForCondition` or similar.
Dave Hunt
A: 

Here is what SeleniumIDE shows in its Reference section:

Arguments:

  • attributeLocator - an element locator followed by an @ sign and then the name of the attribute, e.g. "foo@bar"

So, Dave's answer is correct.

ruby fu novice
+1  A: 

Sounds like the problem isn't with the attribute, but with the basic locator itself. I suggest you play with the $x function in Firebug and make sure that locating the element first (not the attribute) works.

Patrick Lightbody