Skip to content

selenium_webdriver_commands

A command instructs Selenium WebDriver what action to perform on the browser or web elements. WebDriver commands can be broadly categorized:

  • Browser Commands: Manage the browser window itself (navigate, maximize, close).
  • Element Interaction Commands: Perform actions on web elements (click, type text, clear).
  • Information Commands (Getters): Retrieve information about elements or the browser (get text, get attribute, get title).
  • Wait Commands: Pause execution until certain conditions are met (explicit waits, implicit waits - use with caution).

To interact with an element, WebDriver first needs to find it on the web page. This is done using various ‘locator strategies’. You can inspect elements and test locators using your browser’s built-in Developer Tools (usually opened by pressing F12).

Common locator strategies include:

  • By ID: By.id("elementId") - Locates an element using its unique id attribute. Often the fastest and most reliable strategy.
  • By Name: By.name("elementName") - Locates an element using its name attribute. Useful for form elements.
  • By Class Name: By.className("elementClassName") - Locates elements using their class attribute. Note: If multiple classes exist, use only one.
  • By Tag Name: By.tagName("htmlTagName") - Locates elements by their HTML tag (e.g., <a>, <div>, <table>). Often returns multiple elements.
  • By Link Text: By.linkText("Exact Link Text") - Locates an anchor (<a>) element by the exact visible text.
  • By Partial Link Text: By.partialLinkText("Partial Text") - Locates an anchor (<a>) element by a partial match of its visible text.
  • By CSS Selector: By.cssSelector("cssSelectorSyntax") - Locates elements using CSS selector patterns. Very powerful and widely supported.
  • By XPath: By.xpath("xpathExpression") - Locates elements using XPath expressions. Powerful for complex DOM traversal, but can be slower and more brittle than CSS selectors if not written carefully.

Choosing the right locator is crucial for stable tests. Prefer unique IDs, names, or well-structured CSS selectors over complex XPath or relying solely on visible text.