• Selenium Video Tutorials

Selenium WebDriver - 瀏覽器命令



Selenium Webdriver 提供了多個命令,這些命令有助於開啟瀏覽器,在開啟的瀏覽器上執行某些操作,以及退出瀏覽器。

Selenium Webdriver 中的基本瀏覽器命令

下面討論了一些瀏覽器命令:

get(String URL) 命令

此命令開啟瀏覽器,然後啟動作為引數傳遞的 URL。

語法

driver.get("https://tutorialspoint.tw/selenium/practice/nestedframes.php");

getTitle() 命令

此命令獲取作為字串的焦點瀏覽器頁面標題。它不接受任何引數。

語法

String str = driver.getTitle();

getCurrentUrl() 命令

此命令獲取作為字串的焦點瀏覽器 URL。它不接受任何引數。

語法

String str = driver.getCurrentUrl();

getPageSource() 命令

此命令獲取作為字串的焦點瀏覽器頁面原始碼。它不接受任何引數。

語法

String str = driver.getPageSource();

close() 命令

此命令僅關閉焦點瀏覽器視窗。它不接受任何引數,也不返回任何內容。

語法

driver.close();

quit() 命令

此命令關閉與驅動程式會話相關的所有內容(瀏覽器和所有後臺驅動程式程序)。它不接受任何引數,也不返回任何內容。

語法

driver.quit();

如何在網頁上檢查元素?

首先右鍵單擊網頁,然後在 Chrome 瀏覽器中單擊“檢查”按鈕。接下來將開啟完整頁面的 HTML 程式碼。要調查頁面上的特定元素,請單擊可見 HTML 程式碼頂部左側的向上箭頭,如下所示。

Selenium Browser Commands 1

示例 1

讓我們以以下頁面為例,我們首先啟動一個具有 URL 的應用程式:Selenium 自動化練習表單,然後獲取瀏覽器標題Selenium Practice - 學生登錄檔單

請注意,我們可以在 HTML 程式碼中的title標籤內獲取網頁的瀏覽器標題,該標籤位於head標籤內。在下圖中,我們看到頁面的標題是Selenium Practice - 學生登錄檔單

Selenium Browser Commands 2

然後,我們將單擊登入按鈕,之後我們將導航到另一個頁面,該頁面的瀏覽器標題為Selenium Practice - 登入,URL 為:Selenium 自動化練習表單。最後,我們將退出 Web 驅動程式會話。

Selenium Browser Commands 3

程式碼實現

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 BrowserCommand {
   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.tw/selenium/practice/selenium_automation_practice.php");

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

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

      // identify 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());

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

      // Quitting browser
      driver.quit();
   }
}

輸出

Getting browser title after launch: Selenium Practice - Student Registration Form
Getting URL after launch: 
https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php
Getting browser title after clicking link: Selenium Practice - Login
Getting URL after clicking link: 
https://tutorialspoint.tw/selenium/practice/login.php

Process finished with exit code 0

在上面的示例中,我們在開啟的瀏覽器中啟動了一個 URL,並分別獲取了瀏覽器標題和當前 URL,並在控制檯中顯示訊息 - 啟動後獲取瀏覽器標題:Selenium Practice - 學生登錄檔單和啟動後獲取 URL:Selenium 自動化練習表單

然後我們單擊“登入”,並在導航後接收瀏覽器標題和當前 URL,並在控制檯中顯示訊息 - 單擊連結後獲取瀏覽器標題:Selenium Practice - 登入和單擊連結後獲取 URL:Selenium 自動化練習表單。接下來,我們必須退出驅動程式會話。

最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。

示例 2

讓我們再舉一個例子,如下圖所示,我們將單擊新標籤頁

Selenium Browser Commands 4

之後,我們將獲得另一個包含文字新標籤頁的視窗。

Selenium Browser Commands 5

然後我們將關閉包含文字新標籤頁的新視窗,並返回到原始視窗並在那裡訪問文字 - 瀏覽器視窗。最後,我們將退出會話。

Selenium Browser Commands 6

程式碼實現

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.Set;
import java.util.concurrent.TimeUnit;

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

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

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

      // Opening the webpage where we will open a new window
      driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php");

      // click button and navigate to next window
      WebElement b =  driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/button[1]"));
      b.click();

      // Get the window handle of the original window
      String oW = driver.getWindowHandle();

      // get all opened windows handle ids
      Set<String> windows = driver.getWindowHandles();

      // Iterating through all window handles
      for (String w : windows) {
         if(!oW.equalsIgnoreCase(w)) {

            // switching to child window
            driver.switchTo().window(w);

            // accessing element in new window
            WebElement e = driver.findElement
               (By.xpath("/html/body/main/div/div/h1"));
            System.out.println("Text in new window is: " + e.getText());

            // closing new window
            driver.close();
            break;
         }
      }
      // switching to parent window
      driver.switchTo().window(oW);

      // accessing element in parent window
      WebElement e1 = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/h1"));
      System.out.println("Text in parent window is: " + e1.getText());

      // quitting the browser session
      driver.quit();
   }
}

輸出

Text in new window is: New Tab
Text in parent window is: Browser Windows

在上面的示例中,我們捕獲了新視窗上的文字,並在控制檯中收到訊息 - 新視窗中的文字為:新標籤頁。然後我們關閉了子視窗並切換回父視窗。最後,在控制檯中獲取父視窗上的文字 - 父視窗中的文字為:瀏覽器視窗

示例 3

讓我們再舉一個以上頁面的例子,我們將使用getPageSource()方法獲取頁面原始碼。

package org.example;

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

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

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

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

      // Opening the webpage where we will open a new window
      driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php");

      // Getting browser page source
      System.out.println("Getting page source: " + driver.getPageSource());

      // quitting the browser session
      driver.quit();
   }
}

在上面的示例中,我們捕獲了在網頁上啟動的網頁的頁面原始碼,並在控制檯中顯示訊息。

結論

這結束了我們對 Selenium WebDriver 瀏覽器命令教程的全面介紹。我們首先描述了 Selenium Webdriver 中的基本瀏覽器命令,如何在網頁上檢查元素,並逐步介紹瞭如何使用 Selenium Webdriver 使用瀏覽器命令的示例。這使您對 Selenium WebDriver 瀏覽器命令有了深入的瞭解。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。

廣告

© . All rights reserved.