Selenium, CssSelector : Part II [dot(.) notation]


In Selenium, the dot (.) notation is used in CSS selectors to represent a class. This notation allows you to select elements based on their class attribute.

For example, if you have an HTML element like this:

<input id="wpPassword2" name="wpPassword" size="20" class="loginPassword mw-userlogin-password cdx-text-input__input" placeholder="Enter a password" tabindex="2" required="" autocomplete="new-password" type="password" aria-autocomplete="list">

I have taken this code snippet from this link- https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&returnto=Form . It indicates a text-box to enter password .We have passed a value to the text box.

driver.navigate().to("https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&returnto=Form");

driver.findElement(By.cssSelector("input.loginPassword.mw-userlogin-password.cdx-text-input__input")).sendKeys("12345");
  • driver.navigate().to("https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&returnto=Form"): Directs the browser to the Wikipedia account creation page.
  • driver.findElement(By.cssSelector("input.loginText.mw-userlogin-username.cdx-text-input__input")): Finds the username input field using a CSS selector with dot notation.
    • input.loginText.mw-userlogin-username.cdx-text-input__input specifies an <input> element with multiple classes.
  • .sendKeys("12345"): Inputs the text “12345” into the located password input field.