找到 456 篇文章 關於軟體測試

如何在 Selenium 中計算網頁表格中的標題數量?

Debomita Bhattacharjee
更新於 2020年6月10日 12:39:18

811 次檢視

可以使用 `findElements()` 方法計算網頁表格中的標題總數。其邏輯是使用表格內的標籤透過 xpath 返回一個網頁元素列表,然後獲取該列表的大小。程式碼實現示例import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class TableHeaderCount {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver",       "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://tutorialspoint.tw/plsql/plsql_basic_syntax.htm";       driver.get(url);     ... 閱讀更多

如何在 Selenium 中計算頁面中連結的總數?

Debomita Bhattacharjee
更新於 2020年6月10日 12:37:42

14K+ 次檢視

可以使用 `findElements()` 方法計算頁面中連結的總數。其邏輯是使用 `anchor` 標籤返回一個網頁元素列表,然後獲取該列表的大小。程式碼實現示例import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class LinkCount {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://tutorialspoint.tw/index.htm";       driver.get(url);       driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);       //使用帶有 anchor 的 tagname       List ... 閱讀更多

如何在 Selenium 中提取網頁元素的文字?

Debomita Bhattacharjee
更新於 2020年6月10日 12:35:36

726 次檢視

Selenium 使用 `getText()` 方法提取網頁元素的文字。使用 `getText()` 的程式碼實現示例import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ExtractText {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://tutorialspoint.tw/index.htm";       driver.get(url);       driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);       //使用帶有 # 的 id 作為 css 表示式       driver.findElement(By.cssSelector("#gsc-i-id1")).sendKeys("Selenium");       // 使用 getText() 將輸入的文字提取到控制檯       System.out.println(“The entered text is:” ... 閱讀更多

如何在 Selenium 中重置或清除編輯框?

Debomita Bhattacharjee
更新於 2020年6月10日 12:30:20

1K+ 次檢視

我們可以使用 `clear()` 方法在 Selenium 中重置或清除編輯框。使用 `clear()` 的程式碼實現示例import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ResetText {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver",    "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://tutorialspoint.tw/index.htm";       driver.get(url);       driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);       //在編輯框中輸入文字       driver.findElement(By.cssSelector("#gsc-i- id1")).sendKeys("Selenium");       Thread.sleep(1000);       // 使用 ... 閱讀更多

Selenium 4.0 中的相對定位符是什麼?

Debomita Bhattacharjee
更新於 2020年6月10日 12:28:12

520 次檢視

Selenium 4.0 中的相對定位符或友好定位符可與元素的 `tagname` 屬性一起使用。above() - 相對於指定元素位於上面的網頁元素。語法 −driver.findElement(withTagName(“”).above(element));below() - 相對於指定元素位於下面的網頁元素。語法 −driver.findElement(withTagName(“”).below(element));toLeftof() - 相對於指定元素位於左側的網頁元素。語法 −driver.findElement(withTagName(“”).toLeftOf(element));toRightOf() - 相對於指定元素位於右側的網頁元素。語法 −driver.findElement(withTagName(“”).toRightOf(element));使用相對定位符的程式碼實現示例import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import static org.openqa.selenium.support.locators.RelativeLocator .withTagName; public class RelLocator {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver ... 閱讀更多

Selenium 中各種重要的異常是什麼?

Debomita Bhattacharjee
更新於 2020年6月10日 12:25:51

213 次檢視

Selenium 中各種重要的異常列在下面 −TimeOutException − 如果操作未在特定時間內完成,則會引發此異常。如果頁面元素即使在等待之後也沒有載入。driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS) ; driver.get(“https://tutorialspoint.tw/index.htm” );在上面的程式中,添加了 15 秒的隱式等待。如果頁面 https://tutorialspoint.tw/index.htm 在 15 秒內未載入,則會引發 TimeOutException。NoSuchElementException − 如果頁面上不存在具有特定屬性的網頁元素,則會發生此異常。此異常類是 NotFoundException 的子類,如果驅動程式未能... 閱讀更多

Selenium 中不同的 Cookie 方法是什麼?

Debomita Bhattacharjee
更新於 2020年6月10日 12:24:12

372 次檢視

Selenium 中不同的 Cookie 方法列在下面 −driver.manage().deleteAllCookies() − 刪除所有 Cookie。driver.manage().deleteCookie(Id) − 刪除特定 Cookie。driver.manage().deleteCookieNamed(CookieName) − 根據名稱刪除特定 Cookie。driver.manage().getCookies() − 返回所有 Cookie。driver.manage().getCookieNamed(CookieName) − 根據名稱返回特定 Cookie。driver.manage().addCookie(Id) − 新增特定 Cookie。使用一些 Cookie 方法的程式碼實現示例import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class CookiesScripting {    public static void main(String[] args) {       // TODO Auto-generated method stub       System.setProperty("webdriver.chrome.driver",    "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       driver.manage().window().maximize();       // ... 閱讀更多

如何在 Selenium 中最大化瀏覽器?

Debomita Bhattacharjee
更新於 2020年6月10日 12:22:43

3K+ 次檢視

我們可以使用 `maximize()` 方法在 Selenium 中最大化瀏覽器。當前活動視窗將使用此方法最大化。程式碼實現示例import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class BrowserMax {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://tutorialspoint.tw/index.htm";       driver.get(url);       // 使用 maximize() 最大化瀏覽器       driver.manage().window().maximize();       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);       driver.findElement(By.partialLinkText("Coding")).click();       driver.close();    } }閱讀更多

Selenium 中 `getWindowHandle()` 和 `getWindowHandles()` 的區別是什麼?

Debomita Bhattacharjee
更新於 2020年6月10日 12:20:39

13K+ 次檢視

`getWindowHandle()` 和 `getWindowHandles()` 方法有一些顯著的區別。driver.getWindowHandles() – 它儲存同時開啟的所有頁面的控制代碼集。driver.getWindowHandle() – 它獲取當前焦點網頁的控制代碼。它獲取活動瀏覽器的地址,其返回型別為 String。程式碼實現示例import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.Set; import java.util.Iterator; import org.testng.annotations.Test; public class WindowHandles{    @Test    public void windowHandle() throws Exception {       System.setProperty("webdriver.chrome.driver", "C:\Selenium\chromedriver.exe");       WebDriver driver = new ChromeDriver();       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);     ... 閱讀更多

如何在 Selenium 中獲取編輯框的值?

Debomita Bhattacharjee
更新於 2020年6月10日 12:17:34

491 次檢視

我們可以透過以下幾種方式在Selenium中獲取編輯框的值:- 使用getText()方法。- 使用JavascriptExecutor類。使用getText()方法的程式碼示例。示例import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class GetValueScripting {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       String url = "https://tutorialspoint.tw/index.htm";       driver.get(url);       driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);       String text = driver.findElement(By.className("gsc-input")).getText();       System.out.println("Extracted text is " + text);       driver.close();   ... 閱讀更多

廣告