Selenium webdriver에서 locator CSS Selector를 사용하여 요소를 찾을 수 있습니다. CSS 표현식을 생성하는 일반적인 표현식은 tagname[attribute='value']입니다. id 및 class 속성을 활용하여 CSS를 만들 수 있습니다.
id를 사용하는 경우 CSS 표현식의 구문은 tagname#id입니다. 예를 들어 CSS 표현식인 input#txt-loc의 경우 input은 태그 이름이고 txt-loc은 id 속성의 값입니다.
클래스 이름의 경우 CSS 표현식의 구문은 tagname.class입니다. 예를 들어 CSS 표현식인 input.txt-cls의 경우 input은 태그 이름이고 txt-cls는 class 속성 값입니다.
웹 요소 요소(부모)의 n 하위 요소(자식)가 있고 n번째 자식을 찾으려면 CSS 표현식의 구문은 nth-of-type(n)입니다.

위의 html에서 부모 ul의 네 번째 li, 즉 Questions and Answers 텍스트가 있는 앵커 요소를 찾으려면 CSS는 ul.reading li:nth-of-type(4)여야 합니다. 마찬가지로 마지막 자식을 식별하려면 CSS가 ul.reading li:last-child여야 합니다.
동적 값이 있는 속성의 경우 ^=기호를 사용하여 속성 값이 특정 텍스트로 시작하는 요소를 식별할 수 있습니다. 예를 들어, input[name^='qa1'] [여기서 input은 tagname이고 name 속성의 값은 qa1로 시작합니다].
동적 값이 있는 속성의 경우 $=기호를 사용하여 속성 값이 특정 텍스트로 끝나는 요소를 식별할 수 있습니다. 예를 들어, input[class$='loc'] [여기서 input은 tagname이고 class 속성의 값은 loc으로 끝납니다].
동적 값이 있는 속성의 경우 *=기호를 사용하여 속성 값에 특정 하위 문자열이 있는 요소를 식별할 수 있습니다. 예를 들어, input[name*='sub'] [여기서 input은 tagname이고 name 속성의 값은 substring sub를 포함합니다].
예시
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class CSSLocator{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//URL launch
driver.get("https://www.linkedin.com/");
//identify element
WebElement m = driver.
findElement(By.cssSelector("input[id='session_key']"));
//enter text
m.sendKeys("Java");
String s = m.getAttribute("value");
System.out.println("Attribute value: " + s);
//close browser
driver.close();
}
}