找到 190 篇文章,關於 Selenium WebDriver

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); ... 閱讀更多

14K+ 次檢視
可以使用 findElements() 方法統計頁面中連結的總數。邏輯是使用錨標籤返回網頁元素列表,然後獲取該列表的大小。程式碼實現示例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); //使用錨標籤 List ... 閱讀更多

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:” ... 閱讀更多

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); //使用 ... 閱讀更多

520 次檢視
Selenium 4.0 中的相對定位符或友好定位符可與元素的標籤名稱屬性一起使用。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 ... 閱讀更多

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 的子類,如果驅動程式無法成功 ... 閱讀更多

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(); // ... 閱讀更多

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(); } }閱讀更多

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); ... 閱讀更多

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("提取的文字是 " + text); driver.close(); ... 閱讀更多