找到 190 篇文章 關於 Selenium WebDriver

237 次瀏覽
我們可以使用 Selenium 擷取網頁螢幕截圖。這是一個三步過程。擷取螢幕截圖是缺陷和故障分析中最重要的一步。首先,我們必須將驅動程式物件轉換為 TakeScreenshot 介面。語法TakesScreenshot s = (TakesScreenshot)driver;接下來,我們必須藉助 getScreenshotAs 方法獲取影像檔案,最後使用 FileUtils.copyFile 方法將檔案複製到某個位置。語法File src=s.getScreenshotAs(OutputType.FILE); FileUtils.copyFile(src, new File("檔案路徑"));示例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 org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.apache.commons.io.FileUtils; import java.io.File; public class ScreenshotCapture{ public static void ... 閱讀更多

638 次瀏覽
我們可以使用 Selenium 在框架內執行 JavaScript。它可以透過 executeScript 方法執行 JavaScript 命令。要執行的命令作為引數傳遞給該方法。接下來,我們必須使用 return 關鍵字從 JavaScript 命令返回的值。我們必須藉助 JavaScript 中的 window.length 命令來計算頁面中框架的數量。語法JavascriptExecutor j = (JavascriptExecutor) driver; int i = Integer.parseInt(j.executeScript("return window.length").toString());示例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 org.openqa.selenium.JavascriptExecutor; public class FramesJS{ public static void main(String[] args) { ... 閱讀更多

808 次瀏覽
我們可以使用 Selenium 錄製影片。Selenium 中沒有預設的錄製影片技術。影片可以透過以下流程捕獲:需要下載 ATUTestRecorder.jar 和 ATUReporter_Selenium_testNG.jar 檔案並儲存在專案資料夾中。接下來,將上述兩個 jar 檔案新增到專案構建路徑。右鍵單擊專案 -> 單擊屬性 -> 選擇 Java 構建路徑 -> 單擊庫選項卡 -> 單擊新增外部 Jar -> 瀏覽器並選擇 ATUTestRecorder.jar 和 ATUReporter_Selenium_testNG.jar -> 單擊應用 -> 單擊確定。在專案中有一個資料夾來儲存影片。示例@BeforeMethod public void bmethod(Method m){ // 日期格式 ... 閱讀更多

7K+ 次瀏覽
我們可以使用 Selenium 選擇日期選擇器。處理日曆控制元件有點困難,因為日期、月份和年份的選擇可以透過不同的 UI 來表示。有時它們由下拉列表或前後控制元件表示。讓我們選擇如下所示的日期選擇器。起始日期 - 結束日期 - 示例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 org.openqa.selenium.support.ui.Select; public class DatePicker{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String frdate = "20"; ... 閱讀更多

572 次瀏覽
我們可以使用 Selenium IDE 關注新視窗。單擊連結後,會開啟一個新標籤頁或視窗。訪問新視窗後,我們可以將其關閉並切換到父視窗。單擊連結執行以下步驟。訪問站點是要單擊的連結的名稱。選擇單擊連結後開啟的視窗。新增事件是單擊連結後開啟的視窗的頁面標題。現在,我們可以在新視窗上執行操作。使用以下步驟關閉新視窗。使用以下步驟返回父視窗。 ... 閱讀更多

3K+ 次瀏覽
在 Selenium 中等待更改的最佳實踐是使用同步概念。可以使用隱式和顯式等待來處理等待。隱式等待是應用於頁面上每個元素的全域性等待。隱式等待的預設值為 0。它也是動態等待,這意味著如果隱式等待為 5 秒,並且元素在第 3 秒可用,則立即執行下一步,無需等待全部 5 秒。如果 5 秒後元素未找到,則 ... 閱讀更多

4K+ 次瀏覽
我們可以使用 Selenium 處理模式對話方塊。模式對話方塊就像一個視窗,它強制使用者在返回實際頁面之前訪問它。它也可以是身份驗證視窗。讓我們使用以下模式對話方塊 - 示例import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ModDialog{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.uitestpractice.com/Students/Switchto"); // 識別元素並單擊 WebElement m = driver .findElement(By.xpath("//button[contains(text(), ... 閱讀更多

577 次瀏覽
我們可以使用 Selenium 始終從輸入元素中刪除預設文字。clear 方法用於刪除當前存在於編輯框或文字區域中的值。Keys.chord 方法以及 sendKeys 也可以使用。Keys.chord 方法允許你一次傳遞多個鍵。鍵或字串組作為引數傳遞給該方法。首先,將 Keys.CONTROL 和 a 作為引數傳遞給 Keys.chord 方法。然後將整個字串作為引數傳遞給 sendKeys 方法。最後,我們必須將 Keys.DELETE 傳遞給 ... 閱讀更多

7K+ 次瀏覽
我們可以使用Selenium關閉彈出視窗。`getWindowHandles` 和 `getWindowHandle` 方法用於處理彈出視窗。`getWindowHandles` 方法用於將所有開啟的視窗控制代碼儲存在 Set 資料結構中。`getWindowHandle` 方法用於儲存當前焦點彈出視窗的視窗控制代碼。迭代視窗控制代碼可以使用迭代器方法。預設情況下,Selenium 驅動程式控制父視窗。為了將驅動程式的焦點切換到子彈出視窗,我們可以藉助 `switchTo().window` 方法。視窗控制代碼……閱讀更多

4K+ 次瀏覽
我們可以使用Selenium檢查滾動位置。要檢查位置,我們將使用JavaScript執行器。我們必須驗證瀏覽器中 `window.pageYOffset` 的值。當URL啟動時,滾動位於頂部,`window.pageYOffset` 的值為0。當我們滾動到某個元素時,`window.pageYOffset` 的值將大於0。語法:JavascriptExecutor j = (JavascriptExecutor) driver; Long v = (Long) j.executeScript("return window.pageYOffset;");示例: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 org.openqa.selenium.JavascriptExecutor; public class ScrollPosition{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); ... 閱讀更多