创建 css 表达式有哪些不同的方法?

王林
王林 转载
2023-08-26 22:57:09 651浏览

创建 css 表达式有哪些不同的方法?

下面列出了创建 css 表达式的不同方法 -

  • 使用类作为 css 选择器

    这将选择所有该特定类的网络元素。 (用 (.) 表示,例如 - .classname)

  • 使用 id 作为 css 选择器。

    这将选择该特定 id 的 Web 元素。 (例如用 (#) 表示 - #ID)

  • 使用标记名和属性值作为选择器。

    这将选择该对象的 Web 元素特定的属性值组合。 (由标记名[attribute='value']表示)

示例

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

以上就是创建 css 表达式有哪些不同的方法?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除