• Selenium Video Tutorials

Selenium WebDriver - 浏览器导航



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

基本的浏览器导航命令

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

driver.navigate().to("应用程序 URL")

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

语法

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

driver.get("应用程序 URL")

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

语法

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

driver.navigate().back()

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

语法

driver.navigate().back();

driver.navigate().forward()

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

语法

driver.navigate().forward();

driver.navigate().refresh()

此方法重新加载网页。

语法

driver.navigate().refresh();

示例 1

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

Selenium Browser Navigation 1

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

Selenium Browser Navigation 2

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

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

代码实现

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://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,并在控制台中获得了浏览器标题消息 - 启动后获取浏览器标题:Selenium Practice - Student Registration Form。然后我们点击了登录链接,并收到了导航页面的浏览器标题,控制台消息为 - 点击链接后获取浏览器标题:Selenium Practice - Login

接下来,我们在浏览器历史记录中后退,并获得了浏览器标题,控制台消息为 - 导航后退后获取浏览器标题:Selenium Practice - Student Registration Form。再次,我们在浏览器历史记录中前进,并检索了浏览器标题,控制台消息为 - 导航前进后获取浏览器标题:Selenium Practice - Login。最后,我们刷新了浏览器并获得了它的标题,控制台消息为 - 浏览器刷新后获取浏览器标题:Selenium Practice - Login

示例 2

让我们来看一个例子,我们首先启动一个 URL 为 - 的应用程序:

https://tutorialspoint.com/selenium/practice/login.php.

Selenium Browser Navigation 3

然后,我们将点击注册链接,点击后,我们将导航到另一个 URL 为 - 的页面:

https://tutorialspoint.com/selenium/practice/register.php.

Selenium Browser Navigation 4

接下来,我们将返回浏览器历史记录,并将 URL 获取为 -

https://tutorialspoint.com/selenium/practice/login.php.

最后,我们将前进到浏览器历史记录,并将 URL 获取为 -

https://tutorialspoint.com/selenium/practice/register.php.

我们还将刷新浏览器,刷新后我们将获得 URL 为

https://tutorialspoint.com/selenium/practice/register.php.

代码实现

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://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://tutorialspoint.com/selenium/practice/login.php
Getting current URL after clicking link:
https://tutorialspoint.com/selenium/practice/register.php
Getting current URL after navigating back:
https://tutorialspoint.com/selenium/practice/login.php
Getting current URL after navigating forward:
https://tutorialspoint.com/selenium/practice/register.php
Getting current URL after browser refresh:
https://tutorialspoint.com/selenium/practice/register.php

在上面的示例中,我们在打开的浏览器中启动了一个 URL,并在控制台中获得了当前 URL 消息 - 启动后获取当前 URL: https://tutorialspoint.com/selenium/practice/login.php

然后我们点击了注册链接,并在控制台中收到了导航后的当前 URL 消息 - 点击链接后获取当前 URL: https://tutorialspoint.com/selenium/practice/register.php

接下来,我们在浏览器历史记录中后退,并在控制台中获得了导航后的当前 URL 消息 - 导航后退后获取当前 URL: https://tutorialspoint.com/selenium/practice/login.php

再次,我们在浏览器历史记录中前进,并在控制台中收到了导航后的当前 URL 消息 - 导航前进后获取当前 URL: https://tutorialspoint.com/selenium/practice/register.php

最后,我们刷新了浏览器,并在控制台中收到了当前 URL 消息 - 浏览器刷新后获取当前 URL: https://tutorialspoint.com/selenium/practice/register.php

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

广告
© . All rights reserved.