xpath not recognized in selenium, but is recognized in XPather
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 i开发者_如何转开发t will not run in my code. If anyone can see any error in my code, please let me know.
Thanks
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");
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.
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.
精彩评论