CSS 표현식을 만드는 다양한 방법은 다음과 같습니다. -
-
클래스를 CSS 선택기로 사용
이렇게 하면 해당 특정 클래스의 모든 웹 요소가 선택됩니다. (예:.classname(.)으로 표시)
-
id를 CSS 선택기로 사용.
그러면 해당 특정 ID의 웹 요소가 선택됩니다. (예:#ID)
-
태그 이름과 속성 값을 선택자로 사용합니다.
그러면 특정 속성 값 조합의 웹 요소가 선택됩니다. (태그명 [속성='값']으로 표시)
예시
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class CssExpression {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.tutorialspoint.com/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
//Using class with . For css expression
driver.findElement(By.cssSelector(".gsc- input")).sendKeys("Selenium");
driver.close();
}
} 예시
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class CssId {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.tutorialspoint.com/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
//Using id with # for css expression
driver.findElement(By.cssSelector("#gsc-i- id1")).sendKeys("Selenium");
driver.close();
}
} 예시
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class CssTagExp {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.tutorialspoint.com/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
//Using id tagname attribute combination for css expression
driver.findElement(By.cssSelector("input[name=’search’]")).
sendKeys("Selenium");
driver.close();
}
}