Basic 6 patterns of xpath
//tag[@attribute=‘value’] e.g. //input[@name=‘lastname’] Locate all input tag element which has name = ‘lastname’ attribute.
//tag[text()=‘value’] e.g. //option[text()=‘Jul’] Locate all option tag element which has text value of ‘Jul’.
//tag[contains(@attribute,‘value’)] e.g. //input[contains(@name,‘passwd’)] Locate all input tag element which has name attribute containing ‘passwd’.
//tag[contains(text(),‘value’]) e.g. //a[contains(text(),‘Facebook’)] Locate all a tag element which has text value containing ‘Facebook’.
//tag[starts-with(@attribute,‘value’)] e.g. //input[starts-with(@id,‘html5_’)] Locate all input tag element which has id attribute starting with ‘html5_’.
//tag[starts-with(text(),‘value’]) e.g. //a[starts-with(text(),‘Facebook’)] Locate all a tag element which has text value starting with ‘Facebook’.
Advanced 3 patterns of xpath
and Identify element by using multiple attribute values or text values.
hierarchy Identify element by using parent element’sinformation.
following Identify element by using the relation between target element and another element which exists before target element.
Example of 1: and
//a[text()=‘Add New’ and @class=‘page-title-action’]
Locate all elements which meet the following two conditions.
- a tag element which has text value of ‘Add New’
- a tag element which has class = ‘page-title-action’ attribute
Example of 2: hierarchy
//select[@id=‘month’]/option[@value=‘7’]
Locate all elements which meet the following two conditions. - option tag element which has value = ‘7’ attribute
- parent element’s tag is select and the parent element has id = ‘month’ attribute
Example of 3: following
//label[text()=‘File Upload’]//following::a[text()=‘Google’]
Locate all elements which meet the following two conditions. - a tag element which exists after label tag element which has text value of ‘File
Upload’ - a tag element which has text value of ‘Google’