Junior — Middle
Could you list the different types of element search methods in Selenium?
sobes.tech AI
Answer from AI
In Selenium, various methods are used to locate elements on a page, allowing you to find elements by different attributes and DOM structures. The main types of search methods are:
- By.id — search by the unique id attribute.
- By.name — search by the name attribute.
- By.className — search by the CSS class name.
- By.tagName — search by the HTML tag name.
- By.linkText — search by the full link text (the
<a>tag). - By.partialLinkText — search by part of the link text.
- By.cssSelector — search by CSS selector, a very flexible and powerful method.
- By.xpath — search by XPath expression, allowing to find elements based on complex structures and conditions.
Example usage in Java:
WebElement element = driver.findElement(By.id("submit-button"));
WebElement link = driver.findElement(By.linkText("Подробнее"));
WebElement elementByCss = driver.findElement(By.cssSelector("div.content > ul > li.active"));