Trang chủ Kiểm thử phần mềm How to write an XPath to locate element

How to write an XPath to locate element

0

As an automation tester, writing an XPath expression to locate elements is an essential skill that can help you efficiently and accurately automate tests. XPath is a language used for locating nodes in an XML document or an HTML document, and it provides a way of navigating through the HTML structure to identify elements.

Type Selector


Select elements by their type
//A
Selects all elements of type A. Type refers to the type of tag, so div, p and ul are all different element types.
Examples
//div selects all div elements.
//p selects all p elements.

Absolute Path Selector


Select element by their absolute path
//A/B
Selects all elements of type B with Parent A.
Examples
//div/a will select all a elements. within a div.

Relative Selector

Select elements by using relative path
//*
Selects all elements in relative path. Eg. //* selects all elements on a page.
Examples
//* will select all elements.

Relative Selector


Select elements by using relative path
///A Selects all elements in relative path. Eg. ///div selects all elements on a page which are divs and have a parent.
Examples
//*/div will select all div elements with atleast one parent element

Attribute Selector

Select elements with an Attribute
//[@id=’Element ID’]

Selects the element with the id attribute. You can also combine the ID selector with the type selector. Works on all attributes E.g. class, name, placeholder

Examples //[@id=”Element ID”] will select any element with id=”cool”
//ul[@id=”long”] will select

Combine the Descendant & ID Selectors


//[@id=’id’]/A You can combine any selector with the descendent selector. Examples //[@id=”cool”]/span will select all elements that are inside of elements with id=”cool”

Class Selector


Select elements by their class
//[contains(@Attribute,’value’)] The contains selector selects all elements with that attribute which contains given value. Elements can only have one ID, but may contain many classes. Examples //[contains(@class,”neato”)] selects all elements with class=”neato”

More specific Attribute Selector


//A[contains(@Attribute,’value’)]
You can be more specific with Selectors.
Examples
//ul[contains(@class,”important”)] will select all elements that have class=”important stuff”

//input[@placeholder=”Name”] will select all elements with that have placeholder=”Name”

In conclusion, learning how to write effective XPath expressions is an essential skill for automation testers. You can improve your skills and become more confident in targeting elements on a web page. Remember to start with the basics, use the browser’s developer tools, keep your expressions concise, use variables and functions, and practice regularly. With these tips and some persistence, you can become an XPath expert in no time.

Xpath Diner – Where we feast on Xpath Selectors! (topswagcode.com)

0 BÌNH LUẬN

BÌNH LUẬN

Vui lòng nhập bình luận của bạn
Vui lòng nhập tên của bạn ở đây

Exit mobile version