CSS Selector is the most popular locator strategy. Because this can be traversed through attribute and structure of the web page and also this is much optimized locator strategy.
CSS stands for Cascading Style Sheet. This is described the look and formatting of the document written in HTML. This identifies the element based on the tags, id and attributes.
The main advantage of CSS Selector is this is faster than other locators and widely used. This supports both structure and attribute based locators. But this is complex and require deeper learning on the HTML code.
CSS Selector mostly follow the below modes to locate and web element
Tag and ID
Tag and Class
Tag and Attribute
Tag, Class, Attribute
Sub String Matches
driver.findElement(By.cssSelector(<CSS Selector code>))
Scenario with Example:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Locator_By_ID { public static void main(String[] args) { WebDriver driver= new ChromeDriver() ; driver.manage().window().maximize(); driver.get("https://www.testinglpoint.com"); login_button= driver.findElement(By.cssSelector(<CSS Selector code>))