Computer >> Máy Tính >  >> Lập trình >> CSS

Các phương pháp tạo biểu thức css khác nhau là gì?

Các phương pháp tạo biểu thức css khác nhau được liệt kê bên dưới -

  • Sử dụng một lớp làm bộ chọn css

    Thao tác này sẽ chọn tất cả các phần tử web của lớp cụ thể đó. (Được đại diện bởi (.) Chẳng hạn - .classname)

  • Sử dụng id làm bộ chọn css.

    Thao tác này sẽ chọn phần tử web của id cụ thể đó. (Được đại diện bởi (#) chẳng hạn - #ID)

  • Sử dụng tên thẻ và giá trị thuộc tính làm bộ chọn.

    Thao tác này sẽ chọn phần tử web của tổ hợp giá trị thuộc tính cụ thể đó. (Được thể hiện bằng tên thẻ [thuộc tính =’value’])

Ví dụ

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();
      }
}

Ví dụ

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();
   }
}

Ví dụ

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();
   }
}