Selenium 简明教程

Selenium WebDriver - Browser Navigation

Selenium Webdriver 提供了多种方法来实现浏览器导航,包括启动、后退、前进到浏览器历史记录和刷新浏览器。

Basic Browser Navigation Commands

下面讨论了一些浏览器导航方法:

driver.navigate().to("application url")

此方法导航到应用程序 URL。

driver.navigate().to("https://www.tutorialspoint.com/index.htm");

driver.get("application url")

此方法启动浏览器以打开应用程序。

driver.get("https://www.tutorialspoint.com/index.htm");

driver.navigate().back()

此方法根据浏览器历史记录上下文跳转到上一页。

driver.navigate().back();

driver.navigate().forward()

此方法根据浏览器历史记录上下文向前跳转到下一页。

driver.navigate().forward();

driver.navigate().refresh()

此方法重新加载网页。

driver.navigate().refresh();

Example 1

让我们举一个例子,我们首先启动一个浏览器标题为 Selenium Practice - Student Registration Form 的应用程序。

selenium browser navigation 1

然后,我们单击 Login 链接,单击一次后,我们将导航到另一个浏览器标题为 Selenium Practice - Login 的页面。

selenium browser navigation 2

接下来,我们将导航回浏览器历史记录,并将浏览器标题获取为 Selenium Practice - Student Registration Form

最后,我们将向前导航到浏览器历史记录,并将浏览器标题获取为 Selenium Practice - Login 。我们还将刷新浏览器,并获取浏览器标题为 Selenium Practice - Login

Code Implementation

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 BrowNavigation {
   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);

      // launching a browser and open a URL
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // Getting browser title after launch
      System.out.println("Getting browser title after launch: " + driver.getTitle());

      // identify the link then click
      WebElement l = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a"));
      l.click();

      // Getting browser title after clicking link
      System.out.println("Getting browser title after clicking link: " + driver.getTitle());

      // navigate back to browser history
      driver.navigate().back();

      // Getting browser title after navigating back
      System.out.println("Getting browser title after navigating back: " + driver.getTitle());

      // navigate forward to browser history
      driver.navigate().forward();

      // Getting browser title after navigating forward
      System.out.println("Getting browser title after navigating forward: " + driver.getTitle());

      // refresh browser
      driver.navigate().refresh();

      // Getting browser title after browser refresh
      System.out.println("Getting browser title after browser refresh: " + driver.getTitle());

      // Closing browser
      driver.quit();
   }
}
Getting browser title after launch: Selenium Practice - Student Registration Form
Getting browser title after clicking link: Selenium Practice - Login
Getting browser title after navigating back: Selenium Practice - Student Registration Form
Getting browser title after navigating forward: Selenium Practice - Login
Getting browser title after browser refresh: Selenium Practice - Login

在上面的示例中,我们已经启动了一个已打开的浏览器中的 URL,并使用控制台中的消息 Getting browser title after launch: Selenium Practice - Student Registration Form 获取了浏览器标题。然后,我们点击了登录链接,并使用控制台中的消息 Getting browser title after clicking link: Selenium Practice - Login 接收了导航页面的浏览器标题。

接下来,我们已在浏览器历史记录中向后移动,并使用控制台中的消息 Getting browser title after navigating back: Selenium Practice - Student Registration Form 获取了浏览器标题。再次,我们在浏览器历史记录中向前导航,并使用控制台中 Getting browser title after navigating forward: Selenium Practice - Login 的消息检索了浏览器标题。最后,我们刷新了浏览器,并使用控制台中 Getting browser title after browser refresh: Selenium Practice - Login 的消息获取了它的标题。

Example 2

让我们举一个例子,我们首先启动一个带有以下 URL 的应用程序:

selenium browser navigation 3

然后我们点击 Register 链接,之后我们会导航到另一个页面的网址:

selenium browser navigation 4

然后,我们会返回浏览器历史,获取网址:

最后,我们向前导航到浏览器历史,获取网址:

我们还会刷新浏览器,然后我们会获得网址:

Code Implementation

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 BrowNavigationTo {
   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);

      // launching a browser and navigate to a URL
      driver.navigate().to("https://www.tutorialspoint.com/selenium/practice/login.php");

      // Getting browser title after launch
      System.out.println("Getting current URL after launch: " + driver.getCurrentUrl());

      // identify a link then click
      WebElement l = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[3]/a"));
      l.click();

      // Getting browser title after clicking link
      System.out.println("Getting current URL after clicking link: " + driver.getCurrentUrl());

      // navigate back to browser history
      driver.navigate().back();

      // Getting browser title after navigating back
      System.out.println ("Getting current URL after navigating back: " + driver.getCurrentUrl());

      // navigate forward to browser history
      driver.navigate().forward();

      // Getting browser title after navigating forward
      System.out.println("Getting current URL after navigating forward: " + driver.getCurrentUrl());

      // refresh browser
      driver.navigate().refresh();

      // Getting browser title after browser refresh
      System.out.println("Getting current URL after browser refresh: " + driver.getCurrentUrl());

      // Closing browser
      driver.quit();
   }
}
Getting current URL after launch:
https://www.tutorialspoint.com/selenium/practice/login.php
Getting current URL after clicking link:
https://www.tutorialspoint.com/selenium/practice/register.php
Getting current URL after navigating back:
https://www.tutorialspoint.com/selenium/practice/login.php
Getting current URL after navigating forward:
https://www.tutorialspoint.com/selenium/practice/register.php
Getting current URL after browser refresh:
https://www.tutorialspoint.com/selenium/practice/register.php

在上面的示例中,我们已经打开了浏览器中的网址,并在控制台中获得当前网址和消息: Getting current URL after launch: https://www.tutorialspoint.com/selenium/practice/login.php

然后,我们点击注册链接,然后在导航后收到当前网址和控制台中消息: Getting current URL after clicking link: https://www.tutorialspoint.com/selenium/practice/register.php

接着,我们在浏览器历史中后退并得到当前网址和控制台中消息: Getting current URL after navigating back: https://www.tutorialspoint.com/selenium/practice/login.php

再次,我们在浏览器历史中前进,并在导航后得到当前网址和控制台中消息: Getting current URL after navigating forward: https://www.tutorialspoint.com/selenium/practice/register.php

最后,我们刷新了浏览器,并在控制台中得到当前网址和消息: Getting current URL after browser refresh: https://www.tutorialspoint.com/selenium/practice/register.php

因此,在本教程中,我们已经讨论过如何使用 Selenium Webdriver 执行浏览器导航。