Selector using any attribute: tag-name[attribute-name = 'attribute-value']
If the element that we want to locate is not having either of class or id attributes and having other attributes like type, data-target, href etc., then we can make use of them to locate the elements.
For example
This is most common and useful syntax, we can locate any element in the page using this syntax by taking any of it's attributes
Immediate child selector: >
The > selects the immediate child of the element before the symbol.
For example:
In the above section, if I want to find the list element li which has none of the attributes inside ul unordered list then we can write css selector like
Any child selector: blank-space
The blank-space selects the immediate child of the element before the symbol.
For example:
In the above section, if I want to find the list element li which has none of the attributes inside section then we can write css selector like
nth-child selector: :nth-child()
When a written css is locating more than one element, and if want to select an element based on it's position the we can use
In the above section, if I want to find the first list element li which has none of the attributes inside ul unordered list then we can write css selector like
WebElement element = driver.findElement(By.cssSelector("input[type='submit']"));
WebElement element = driver.findElement(By.cssSelector
("a[href='https://www.google.co.in/']"));
WebElement element = driver.findElement(By.cssSelector(".list-group>li"));
// In this .list-group will find the ul
// Then >li matches both li elements below that ul
WebElement element = driver.findElement(By.cssSelector("#about li");
// In this #about will find the section
// Followed bya blank space li matches both li elements below that ul
WebElement element = driver.findElement(By.cssSelector(".list-group>li:nth-child(1)"));
// In this .list-group will find the ul
// Then >li matches both li elements below that ul
//nth-child(1) matched the first li element.