Selenium教程

Selenium 定位策略-(通过CSS标签,类和属性)

在本节中,您将学习如何使用CSS-子字符串匹配技术来定位特定的网络元素。
WebDriver提供了一个有趣的功能,允许使用^,$和*进行部分字符串匹配。

1、以(^)开头:

要选择和定位网络元素,我们将使用^表示"以...开头"。
请按照下面给出的步骤使用局部字符串匹配技术。
打开URL: https://www.testandquiz.com/selenium/testing.html 右键单击示例网页上的"文本框",然后选择"检查元素" 定位策略-通过CSS-子字符串匹配 它将启动一个窗口,其中包含开发文本框所涉及的所有特定代码。 定位策略-通过CSS-子字符串匹配 记下其tag和id属性。 定位策略-通过CSS-子字符串匹配
用于通过CSS定位Web元素的Java语法-使用^运算符的子字符串匹配技术写为:
driver.findElement(By.cssSelector("Tag[attribute^=prefix of the string]"))
    
因此,为了在示例网页上定位文本框,我们将使用输入标签以及id属性:
driver.findElement(By.cssSelector("input[id^='fna']"))
    
类似地,为了在示例网页上找到Submit按钮,我们将使用button标记以及id属性:
driver.findElement(By.cssSelector("button[id^='idOf']"))
    
我们为您创建了一个示例脚本,以使您更好地了解如何通过CSS-使用^运算符的子字符串匹配技术来定位Web元素。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SampleFive {
    public static void main(String[] args) {
        
        // System Property for Chrome Driver 
        System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
        // Instantiate a ChromeDriver class.    
        WebDriver driver=new ChromeDriver();
        // Launch Website
        driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 
    // Click on the textbox and send value
        driver.findElement(By.cssSelector("input[id^='fna']")).sendKeys("lidihuo JMeter Tutorial");
             
    // Click on the Submit button using click() command
    driver.findElement(By.cssSelector("button[id^='idOf']")).click();
        
    // Close the Browser
        driver.close();
         
    }
}
    

2、以($)结尾:

要选择和定位Web元素,我们将使用$表示"以...结尾"。
请按照下面给出的步骤使用局部字符串匹配技术。
打开URL: https://www.testandquiz.com/selenium/testing.html 右键单击示例网页上的"文本框",然后选择"检查元素" 定位策略-通过CSS-子字符串匹配 它将启动一个窗口,其中包含开发文本框所涉及的所有特定代码。 定位策略-通过CSS-子字符串匹配 记下其tag和id属性。 定位策略-通过CSS-子字符串匹配
用于通过CSS定位Web元素的Java语法-使用$运算符的子字符串匹配技术写为:
driver.findElement(By.cssSelector("Tag[attribute$=suffix of the string]"))
    
因此,为了在示例网页上找到"文本"框,我们将使用输入标签以及id属性:
driver.findElement(By.cssSelector("input[id$='me']"))
    
类似地,为了在示例网页上定位Submit按钮,我们将使用button标记以及id属性:
driver.findElement(By.cssSelector("button[id$='on']"))
    
我们为您创建了一个示例脚本,以使您更好地了解如何通过CSS-使用$运算符的子字符串匹配技术来定位Web元素。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SampleSix {
    public static void main(String[] args) {
        
        // System Property for Chrome Driver 
        System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
        // Instantiate a ChromeDriver class.    
        WebDriver driver=new ChromeDriver();
        // Launch Website
        driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 
    // Click on the textbox and send value
        driver.findElement(By.cssSelector("input[id$='me']")).sendKeys("lidihuo Data Structure Tutorial");
             
    // Click on the Submit button using click() command
    driver.findElement(By.cssSelector("button[id$='on']")).click();
        
    //  Close the Browser
        driver.close();
         
    }
}
    

3、包含(*):

要选择和定位Web元素,我们将使用*表示"子字符串"。
请按照以下步骤使用"部分字符串匹配技术。
打开URL: https://www.testandquiz.com/selenium/testing.html 右键单击示例网页上的"文本框",然后选择"检查元素" 定位策略-通过CSS-子字符串匹配 它将启动一个窗口,其中包含开发文本框所涉及的所有特定代码。 定位策略-通过CSS-子字符串匹配 记下其tag和id属性。 定位策略-通过CSS-子字符串匹配
用于通过CSS定位Web元素的Java语法-使用*运算符的子字符串匹配技术写为:
driver.findElement(By.cssSelector("Tag[attribute*=sub-string]"))
因此,为了在示例网页上找到"文本"框,我们将使用输入标签以及id属性:
driver.findElement(By.cssSelector("input[id*='id']"))
我们还可以使用'contains()'代替*运算符
driver.findElement(By.cssSelector("input:contains('id')"))
我们为您创建了一个示例脚本,以更好地理解如何通过CSS-使用$运算符的子字符串匹配技术来定位Web元素。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SampleSeven {
    public static void main(String[] args) {
        
        // System Property for Chrome Driver 
        System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
        // Instantiate a ChromeDriver class.    
        WebDriver driver=new ChromeDriver();
        // Launch Website
        driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 
    // Click on the textbox and send value
        driver.findElement(By.cssSelector("input[id*='id']")).sendKeys("lidihuo C++ Tutorial");
             
    // Close the Browser
        driver.close();
         
    }
}

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4