Selenium教程

Selenium 定位策略-(通过部分链接文字)

在本节中,您将学习如何使用特定的Web元素的部分链接文本来定位。
让我们考虑一个测试案例,在该案例中,我们将自动执行以下方案:
调用Chrome浏览器 打开URL: https://www.testandquiz.com/selenium/testing.html 单击示例网页上的链接文本"这是一个链接"
我们将逐步创建测试用例,以使您全面了解如何使用定位器来识别和定位特定的Web元素。
第1步。。启动Eclipse IDE并打开在本教程前面的课程中创建的现有测试套件" Demo_Test"。
第二步。。右键单击" src"文件夹,然后从 New> Class 创建一个新的Class File。
Selenium Webdriver定位策略通过部分链接文本
将您的班级名称设为" Partial_Link",然后单击"完成"按钮。
Selenium Webdriver通过局部链接文本定位策略
第3步。让我们进入编码基础。
要调用Google Chrome浏览器,我们需要下载ChromeDriver.exe文件并将系统属性" webdriver.chrome.driver"设置为ChromeDriver.exe文件的路径。我们已经在本教程的早期课程中对此进行了讨论。您还可以参考"> "在Firefox浏览器上运行测试" 了解如何下载并设置Chrome驱动程序的系统属性。
以下是为Chrome驱动程序设置系统属性的示例代码:
// System Property for Chrome Driver 
System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
之后,我们必须使用ChromeDriver类初始化Chrome驱动程序。
以下是使用ChromeDriver类初始化Chrome驱动程序的示例代码。
// Instantiate a ChromeDriver class.    
    WebDriver driver=new ChromeDriver();
结合以上两个代码块,我们将获得代码段以启动Google Chrome浏览器。
    // System Property for Chrome Driver 
System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
        // Instantiate a ChromeDriver class.    
    WebDriver driver=new ChromeDriver();
之后,我们需要编写代码以使第二个测试场景自动化(导航到所需的URL)。
以下是示例代码,可导航到所需的URL:
// Launch Website
driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 
到目前为止,完整的代码如下所示:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Partial_Link {
    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"); 
    
    }
}
第4步。现在,我们将尝试使用链接文本的部分值来查找所需的Web元素。在Selenium中,查找特定的Web元素涉及对其HTML代码的检查。
请按照以下步骤在示例网页上找到"复选框"。
打开URL: https://www.testandquiz.com/selenium/testing.html 右键单击示例网页上的"这是链接"文本,然后选择"检查元素" 通过部分链接文本的Selenium Webdriver定位策略 它将打开一个窗口,其中包含开发链接文本所涉及的所有特定代码。 Selenium Webdriver通过局部链接文本定位策略 选择链接文本的值,即"这是一个链接"。 Selenium Webdriver通过局部链接文本定位策略
通过部分链接文本定位Web元素的Java语法写为:
driver.findElement(By.partialLinkText (<linktext>))
因此,为了在示例网页上找到链接文本,我们将使用其部分链接文本的值:
driver.findElement(By.partialLinkText (<"this is">))
第5步。要使第三个测试场景自动化,我们需要编写代码,然后单击链接文本。
这是单击链接文本的示例代码。
// Click on the Link Text using click() command
 driver.findElement(By.partialLinkText("this is")).click();
Thus, our final test script will look something like this:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Partial_Link {
    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 Link Text using click() command
    driver.findElement(By.partialLinkText("this is")).click();
    }
}
以下屏幕截图显示了我们的测试脚本的Eclipse窗口。
通过部分链接文本的Selenium Webdriver定位策略
第6步。。右键单击Eclipse代码,然后选择运行方式> Java应用程序。
通过部分链接文本的Selenium Webdriver定位策略
执行后,上述测试脚本将启动Firefox浏览器并自动执行所有测试方案。

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