Junior — Middle
How can you identify an element using a CSS or XPath selector?
sobes.tech AI
Answer from AI
Various methods are used to identify elements with CSS selectors, based on the structure and attributes of HTML:
- By tag:
div,input,button, etc. - By class:
.className(e.g.,.btn-primary) - By ID:
#elementId - By attributes:
[type='text'],[name='username'] - Combinations:
div.className,ul > li:first-child
Example of a CSS selector:
input[type='submit'].btn
XPath allows for more flexible and precise element location using paths in the DOM and conditions:
- By tag:
//div - By attribute:
//input[@type='text'] - By text:
//button[text()='Отправить'] - By index:
(//li)[3]— the thirdlielement - Relative to other elements:
//div[@id='main']//a[@class='link']
Example of XPath:
//input[@name='username' and @type='text']
Thus, the choice between CSS and XPath depends on the task and the page structure. CSS is simpler and faster, XPath is more powerful for complex conditions.