Selenium 简明教程
Selenium WebDriver - Browser Navigation
Selenium Webdriver 提供了多种方法来实现浏览器导航,包括启动、后退、前进到浏览器历史记录和刷新浏览器。
Selenium Webdriver provides multiple methods which help to achieve browser navigation which include launching, moving backward, forward on browser history, and refreshing the browser.
Basic Browser Navigation Commands
下面讨论了一些浏览器导航方法:
Some of the browser navigation methods are discussed below −
driver.navigate().to("application url")
此方法导航到应用程序 URL。
This method navigates to an application url.
driver.navigate().to("https://www.tutorialspoint.com/index.htm");
driver.get("application url")
此方法启动浏览器以打开应用程序。
This method launches the browser to open an application.
driver.get("https://www.tutorialspoint.com/index.htm");
driver.navigate().back()
此方法根据浏览器历史记录上下文跳转到上一页。
This method jumps to the previous page as per browser history context.
driver.navigate().back();
Example 1
让我们举一个例子,我们首先启动一个浏览器标题为 Selenium Practice - Student Registration Form 的应用程序。
Let us take an example, where we would first launch an application having the browser title Selenium Practice - Student Registration Form.
然后,我们单击 Login 链接,单击一次后,我们将导航到另一个浏览器标题为 Selenium Practice - Login 的页面。
Then we would click on the Login link, once clicked, we would be navigated to another page having the browser title Selenium Practice - Login.
接下来,我们将导航回浏览器历史记录,并将浏览器标题获取为 Selenium Practice - Student Registration Form 。
Next, we would navigate back to the browser history, and get the browser title as Selenium Practice - Student Registration Form.
最后,我们将向前导航到浏览器历史记录,并将浏览器标题获取为 Selenium Practice - Login 。我们还将刷新浏览器,并获取浏览器标题为 Selenium Practice - Login 。
Finally, we would navigate forward to the browser history, and get the browser title as Selenium Practice - Login. We would also refresh the browser, and obtain the browser title as 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 接收了导航页面的浏览器标题。
In the above example, we had launched a URL in the opened browser and obtained the browser title with the message in the console - Getting browser title after launch: Selenium Practice - Student Registration Form. We had then clicked on the Login link and received the browser title of the navigated page with the message in the console - 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 的消息获取了它的标题。
Next, we had moved back in browser history and obtained the browser title with the message in the console - Getting browser title after navigating back: Selenium Practice - Student Registration Form. Again, we navigated forward in browser history and retrieved the browser title with the message in the console - Getting browser title after navigating forward: Selenium Practice - Login. Finally, we refreshed the browser and got its title with the message in the console - Getting browser title after browser refresh: Selenium Practice - Login.
Example 2
让我们举一个例子,我们首先启动一个带有以下 URL 的应用程序:
Let us take an example, where we would first launch an application having the URL as −
然后我们点击 Register 链接,之后我们会导航到另一个页面的网址:
Then we would click on the Register link, once clicked, we would be navigated to another page having the URL as −
然后,我们会返回浏览器历史,获取网址:
Next, we would navigate back to the browser history, and get the URL as −
最后,我们向前导航到浏览器历史,获取网址:
Finally, we would navigate forward to the browser history, and get the URL as −
我们还会刷新浏览器,然后我们会获得网址:
We would also refresh the browser, after which we would obtain the URL as
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 。
In the above example, we had launched a URL in the opened browser and obtained the current URLwith the message in the console - 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 。
We had then clicked on the Register link and received the current URL after navigation with the message in the console - 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 。
Next, we had moved back in browser history and obtained the current URL after navigation with the message in the console - 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 。
Again, we navigated forward in browser history and received the current URL after navigation with the message in the console - 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 。
Finally, we refreshed the browser and received the current URL with the message in the console - Getting current URL after browser refresh: https://www.tutorialspoint.com/selenium/practice/register.php.
因此,在本教程中,我们已经讨论过如何使用 Selenium Webdriver 执行浏览器导航。
Thus, in this tutorial, we had discussed how to perform browser navigation using the Selenium Webdriver.