Selenium 简明教程

Selenium Webdriver 可用于处理网页上的链接。在 * HTML* 术语中,每个链接(称为超链接)都由称为锚的标记名标识。此外,网页上的每个链接都有一个名为 href 的属性。

现在让我们讨论超链接 * anchor tags* 的 Created 标识,如下面的图片所示。右键单击该网页,然后单击 Chrome 浏览器中的检查按钮。之后,整个页面的相应 HTML 代码将可见。要检查页面上的创建链接,请单击如下面突出显示的左上角箭头。

selenium handling 1

一旦我们单击并将箭头指向创建超链接,其 HTML 代码将可见。

selenium handling 2

可以在 Selenium 中使用链接文本定位符来标识链接。将标识与链接文本匹配值匹配的第一个元素。

Syntax

Webdriver driver = new ChromeDriver();
driver.findElement(By.linkText("value of link text"));

让我们来看上述页面的示例,单击创建链接后,将在页面上看到文本 Link has responded with status 201 and status text Created

selenium handling 3

Example

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class HandLinks {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify an element
      driver.get
      ("https://www.tutorialspoint.com/selenium/practice/links.php");

      // identify link with link text locator then click
      WebElement l = driver.findElement(By.linkText("Created"));
      l.click();

      // identify text locator
      WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[1]"));
      System.out.println("Text appeared is: " + t.getText());

      // Closing browser
      driver.quit();
   }
}
Text appeared is: Link has responded with status 201 and status text Created

Process finished with exit code 0

在上述示例中,在带有消息的链接 Created 上执行单击后获得的文本为 Link has responded with status 201 and status text Created

最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。

可以在 Selenium 中使用部分链接文本定位符来标识链接。将标识与部分链接文本匹配值匹配的第一个元素。

Syntax

Webdriver driver = new ChromeDriver();
driver.findElement(By.partialLinkText("value of partial link text"));

Example

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class HandPartialLinks {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify an element
      driver.get("https://www.tutorialspoint.com/selenium/practice/links.php");

      // identify link with partial link text locator then click
      WebElement l = driver.findElement(By.partialLinkText("Creat"));
      l.click();
      // identify text locator
      WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[1]"));
      System.out.println("Text appeared is: " + t.getText());

      // Closing browser
      driver.quit();
   }
}
Text appeared is: Link has responded with status 201 and status text Created

在上述示例中,通过 partial link text 单击该链接后获得的文本(带有消息)为 Link Link has responded with status 201 and status text Created

可以使用 Selenium 中的标签名称定位器来识别链接。具有标签名称匹配值的第一个元素会被识别。

在上面讨论的示例中,我们单击并指向“已创建”超链接,其 HTML 代码是可见的,反映了锚点标签名称(称为“a”并用 <> 括起)。

<a href="javascript:void(0);" id="created" onclick="shide('create')">Created</a>

Syntax

Webdriver driver = new ChromeDriver();
driver.findElement(By.tagName("a”));

让我们以同一页面的示例为例,我们首先计算链接的总数,然后我们单击某个特定链接,例如“无内容”。在单击该链接后,我们在页面上获取文本 Link has responded with status 204 and status text

selenium handling 4

Example

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;

public class TotalLinks {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify an element
      driver.get("https://www.tutorialspoint.com/selenium/practice/links.php");

      // identify link with link text locator then click
      WebElement l = driver.findElement(By.linkText("No Content"));
      l.click();

      // Retrieve all links using locator By.tagName and storing in List
      List<WebElement> totalLnks = driver.findElements(By.tagName("a") );
      System.out.println( "Total number of links: " + totalLnks.size() ) ;

      // Running loop through list of web elements
      for( int j = 0; j < totalLnks.size(); j ++){
         if( totalLnks.get(j).getText().equalsIgnoreCase("No Content") ) {
            totalLnks.get(j).click();
            WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[2]"));

            // get the browser title to confirm navigation after click
            System.out.println( "Get text after click: " + t.getText());
            break ;
         }
      }

      // Closing browser
      driver.quit();
   }
}
Total number of links: 42
Get text after click: Link has responded with status 204 and status text No Content

在上面的示例中,我们在网页上计算了链接的总数,并在控制台中收到了消息 - Total number of links: 42 ,以及在使用消息 Get text after click: Link has responded with status 204 and status text No Content 执行单击后获得的文本。

Conclusion

这总结了我们对 Selenium Webdriver 处理链接教程的全面了解。我们从描述如何在 HTML 中识别链接开始,并举例说明如何使用 Selenium Webdriver 中的链接文本、部分链接文本和标签名称定位器来处理链接。这使你掌握了 Selenium Webdriver - 处理链接的深入知识。明智的做法是持续实践所学内容,并探索其他与 Selenium 相关的内容,以加深理解并拓展视野。