The By.cssSelector() method is similar to the By.xpath() method in its usage, but the difference is that it is slightly faster than the By.xpath locating mechanism. The following are the commonly used syntaxes to identify elements:
- To identify an element using the div element with the #flrs ID, we use the #flrs syntax
- To identify the child anchor element, we use the #flrs > a syntax, which will return the link element
- To identify the anchor element with its attribute, we use the #flrs > a[a[href="/intl/en/about.html"]] syntax
Let's try to modify the previous code, which uses the XPath locating mechanism to use the cssSelector mechanism:
@Testpublic void byCssSelectorLocatorExample() { WebElement searchBox = ...