Friday, January 14, 2011

XPATH Locators

Pattern Meaning
//E an element of type E
//E[@foo="bar"] an E element whose "foo" attribute value is exactly equal to "bar"
//E[contains(text(),"foo")] an E element containing the substring "foo" in its textual contents
//E[contains(@foo,"value")] an E element containing the substring "value" in the attribute foo
//E[@foo="bar" and @foo1="bar1"] an E element whose "foo" attribute value is exactly equal to "bar" and "foo1" attribute value is exactly equal to "bar1"
//E[descendant::F] an E element which contains the F as its child
//E/following-sibling::F an F element which has an adjacent element as E
//E/preceding-sibling::F an F element which is beside E



IDENTIFY AN ELEMENT USING MULTIPLE ATTRIBUTES

Eg: Using XPATH AND CSS


To combine the css locators for multiple attributes use the following way..
css=E[foo='abc'][foo1='abcs'][foo2='abcd'] and so on...
for xpath locators using and u can combine..
//E[@foo="bar" and @foo1="bar1"]
Use getAttribute method to get any element attribte
See the attribute values using firebug suppose

Div Text


to get the style attribute from the above div use some thing like below.
String divStyle = selenium.getAttribute(XPATHOFDIV@style);
String divStyle = selenium.getAttribute(CSSLOCATOR OF DIV@style);
Eg:: String divStyle = selenium.getAttribute(//div[@id='one']@style);
or use CSS locators too..
String divStyle = selenium.getAttribute(css=div[id='one']@style);

2 comments: