在Selenium中通过文本查找元素,实现部分文本匹配

发表于:2023-1-09 09:39

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:迪鲁宾    来源:掘金

  在本文关于如何在Selenium WebDriver中按文本查找元素的前一个例子中,你看到了如何使用findElement by Text来进行完整的文本匹配。在本节中,我们将了解如何使用部分文本匹配来定位网络元素。
  同时阅读-在Selenium中用链接文本和部分链接文本查找元素
  使用案例
  1. 登录到Selenium Playground。
  2. 识别所有名称中含有表格的网络元素。
  3. 打印所有这些Web元素的文本。
  让我们看看上述测试案例的定位器。
  实施
  如上图所示,我们使用Table文本和它的标签a进行部分匹配,结果,我们使用上述定位器得到了总共5个Web元素。由于有超过1个的Web元素,在这种情况下,我们将使用FindElements。
  Selenium中的FindElements会返回与定位器值相匹配的Web元素的列表,不像FindElement那样只返回一个Web元素。如果网页中没有匹配的元素,FindElements会返回一个空列表。
  Selenium中FindElements的语法是。
  List<WebElement> listName= driver.findElements(By.<LocatorStrategy>(“LocatorValue”))
  因此,在这里使用部分文本匹配的FindElements的正确实现是。
  List<WebElement> tableOptions=driver.findElements(By.xpath(“//a[contains(text(),’Table’)”)
  现在让我们使用同样的方法来编写我们的测试案例。
  你可以参考下面的测试案例。
  package LambdaTest;
   
      import org.openqa.Selenium.By;
      import org.openqa.Selenium.WebElement;
      import org.openqa.Selenium.remote.DesiredCapabilities;
      import org.openqa.Selenium.remote.RemoteWebDriver;
      import org.testng.Assert;
      import org.testng.annotations.AfterTest;
      import org.testng.annotations.BeforeTest;
      import org.testng.annotations.Listeners;
      import org.testng.annotations.Test;
   
      import java.net.MalformedURLException;
      import java.net.URL;
      import java.util.List;
   
      @Listeners({util.Listener.class})
      class AutomationUsingFindElementByText {
   
          public String username = "YOUR USERNAME";
          public String accesskey = "YOUR ACCESSKEY";
          public static RemoteWebDriver driver = null;
          public String gridURL = "@hub.lambdatest.com/wd/hub";
   
          @BeforeTest
          public void setUp() throws Exception {
             DesiredCapabilities capabilities = new DesiredCapabilities();
              capabilities.setCapability("browserName", "chrome");
              capabilities.setCapability("version", "96.0");
              capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
              capabilities.setCapability("build", "AutomationUsingFindElement");
              capabilities.setCapability("name", "AutomationUsingFindElementSuite");
              try {
                  driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
              } catch (MalformedURLException e) {
                  System.out.println("Invalid grid URL");
              } catch (Exception e) {
                  System.out.println(e.getMessage());
              }
          }
   
          @Test
          public void findElementByPartialTextMatch() {
              try {
                  System.out.println("Logging into Lambda Test Selenium Playground");
                  driver.get("http://labs.lambdatest.com/Selenium-playground/");
                  List<WebElement> tableOptions= driver.findElements(By.xpath("//a[contains(text(),'Table')]"));
                  for(WebElement e: tableOptions){
                      System.out.println("The different options with table in name are:"+e.getText());
                  }
   
              } catch (Exception e) {
   
              }
   
          }
   
          @AfterTest
          public void closeBrowser() {
              driver.close();
              System.out.println("The driver has been closed.");
   
          }
   
      }
  你可以使用下面的testng.xml文件来运行上述测试案例。
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
  <suite  name="AutomationUsingFindElementSuite">
   
      <test name="AutomationUsingFindElementTest" >
          <classes>
              <class name="LambdaTest.AutomationUsingFindElementByText" >
              </class>
          </classes>
      </test>
  </suite>
  还有下面的pom.xml文件用于安装所有必要的依赖项。
  <?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
   
      <groupId>org.example</groupId>
      <artifactId>LambdaTest</artifactId>
      <version>1.0-SNAPSHOT</version>
      <dependencies>
          <dependency>
              <groupId>org.Seleniumhq.Selenium</groupId>
              <artifactId>Selenium-api</artifactId>
              <version>4.0.0-alpha-7</version>
          </dependency>
          <dependency>
              <groupId>org.Seleniumhq.Selenium</groupId>
              <artifactId>Selenium-remote-driver</artifactId>
              <version>4.0.0-alpha-7</version>
          </dependency>
          <dependency>
              <groupId>org.Seleniumhq.Selenium</groupId>
              <artifactId>Selenium-chrome-driver</artifactId>
              <version>4.0.0-alpha-7</version>
          </dependency>
          <dependency>
              <groupId>org.testng</groupId>
              <artifactId>testng</artifactId>
              <version>6.14.3</version>
          </dependency>
          <dependency>
              <groupId>io.github.bonigarcia</groupId>
              <artifactId>webdrivermanager</artifactId>
              <version>4.4.3</version>
          </dependency>
      </dependencies>
   
      <properties>
          <maven.compiler.source>8</maven.compiler.source>
          <maven.compiler.target>8</maven.compiler.target>
      </properties>
   
  </project>
  代码演练
  在这一节中,关于如何在Selenium中通过文本查找元素,现在让我们详细检查一下测试案例的演练。BeforeTest和import语句与我们在之前的例子中看到的保持一致。
  @Test(findElementByPartialTextMatch)。在这种情况下,我们首先登录到Selenium Playground网页。之后,我们找到所有文本中含有Table的Web元素,并将其存储在一个列表中。之后,我们对该列表进行迭代,并打印其文本。
  一旦测试完成,你也可以在LambdaTest自动化仪表板上查看你的测试结果、日志和测试记录
  你还可以在LambdaTest分析仪表板上看到测试结果。该仪表板显示与您的测试有关的所有细节和指标。
  导航到LambdaTest分析仪表板以查看您的测试指标。您可以从测试概述中快速评估测试性能和整体健康状况。测试概述将显示你的团队运行了多少个通过和失败的测试以及这些测试的整体效率。
  控制台输出
  一旦你运行了测试用例,控制台的输出将看起来像下面这样。
  如果你是一名开发人员或测试人员,并希望将你的技能提高到一个新的水平,LambdaTest的这个Selenium 101认证可以帮助你达到这个目标。
  下面是LambdaTest的Selenium 101认证的一个简短介绍。
  结论
  在这个关于如何在Selenium WebDriver中通过文本查找元素的Selenium Java教程中,我们探讨了在Selenium中使用文本查找元素。我们看到,在完全和部分文本匹配的情况下,我们如何使用text()方法。我们还看到了如何在FindElements的情况下使用它,并通过文本匹配获得一个Web元素的列表。最后,我们还在云Selenium网格上用Java实现了这些案例。
  说实话,当涉及到定位Web元素时,使用text()是我个人在Selenium中最喜欢的方法之一,因为它非常容易实现,而且可以以任何方式调整以符合我们的用例。
  我希望你喜欢阅读这篇关于如何在Selenium中通过文本查找元素的文章,了解更多关于FindElement By Text的信息,我相信这个方法也会成为你个人的最爱。
  测试愉快!
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号