Selenium, CssSelector : Part I (tag[attribute=’value’])


The following code snippet has been copied from this link https://wikipedia.org

<span class=”lang-list-button-text jsl10n” data-jsl10n=”portal.language-button-text”>Read Wikipedia in your language </span>

It indicates a button with language options. List of language supported on the Wikipedia will be opened if we click on it.
I have used the CssSelector to identify the element and then clicked on it.

driver.get("https://wikipedia.org");
driver.findElement(By.cssSelector("span[class='lang-list-button-text jsl10n']")).click();

Check the syntax in this case tag[attribute=’value’]

  • driver: Represents the WebDriver instance that interacts with the browser.
  • findElement(By.cssSelector(...)): Locates a web element using a CSS selector.
  • "span[class='lang-list-button-text jsl10n']": The CSS selector used to identify the <span> element with the specified class attributes, indicating the language button.
  • .click(): Simulates a mouse click on the identified element, triggering the associated action.