找到關於 Selenium WebDriver 的 190 篇文章

Selenium 中有哪些不同型別的等待?

Debomita Bhattacharjee
更新於 2020年6月10日 13:51:04

686 次瀏覽

Selenium 中可用的不同型別的等待如下所示:隱式等待這是 Selenium 中的一種動態等待,語法如下:driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);顯式等待這是 Selenium 中的一種動態等待,語法如下:WebDriverWait w = new WebDriverWait(driver, ); w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("")));流暢等待這是 Selenium 中的一種動態等待,語法如下:Wait w = new FluentWait(driver).withTimeout(Duration.ofSeconds(30)) .pollingEvery(Duration.ofSeconds(3)).ignoring(NoSuchElementException.class);靜態等待用於將執行暫停指定時間段。示例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 ThreadWait {    public static void main(String[] args) throws InterruptedException ... 閱讀更多

如何在 Selenium 中設定瀏覽器視窗的大小?

Debomita Bhattacharjee
更新於 2020年6月10日 13:46:08

4K+ 次瀏覽

我們可以透過以下方法設定瀏覽器視窗的大小:getSize() 方法Javascript 執行器示例使用 setSize() 方法。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 BrowserDimension {    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);       // 使用 getSize() 獲取當前視窗大小       System.out.println(driver.manage().window().getSize());       // 建立 Dimensions 物件 ... 閱讀更多

如何在 Selenium 中執行頁面滾動操作?

Debomita Bhattacharjee
更新於 2020年6月10日 13:44:08

1K+ 次瀏覽

我們可以對 Selenium 中的滾動執行以下操作:垂直滾動向下滾動到特定畫素。JavascriptExecutor j = (JavascriptExecutor) driver; // 使用 x、y 軸上的座標 0 和 1500 向下滾動 1500 畫素 j.executeScript("window.scrollBy(0, 1500)");向下滾動到頁面底部。JavascriptExecutor j = (JavascriptExecutor) driver; // 向下滾動到頁面底部 js.executeScript("window.scrollTo(0, document.body.scrollHeight)");示例垂直向下滾動,直到元素可見。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 org.openqa.selenium.JavascriptExecutor; public class ScrollDownVisible {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");   ... 閱讀更多

如何在 Selenium 中提取元素的屬性值?

Debomita Bhattacharjee
更新於 2020年6月10日 13:35:43

1K+ 次瀏覽

我們可以使用 getAttribute() 方法在 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; import java.util.List; public class AttributeType {    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 標籤名屬性組合 ... 閱讀更多

如何在 Selenium 中在編輯框內按 Enter 鍵?

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

729 次瀏覽

Selenium 提供列舉鍵宏來執行 Enter 操作。示例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 PressEnter {    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);       // 透過 sendKeys() 方法傳遞 Keys.ENTER       driver.findElement(By.className("gsc-input")).sendKeys("Keys.ENTER");       driver.close();    } }閱讀更多

如何在 Selenium 中執行瀏覽器導航?

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

568 次瀏覽

Selenium 中有各種 navigate() 方法可以執行導航。它們如下所示:navigate().to(url)用於啟動新的瀏覽器並開啟引數中的特定 URL。navigate().refresh()用於重新載入頁面。navigate().back()用於根據瀏覽器歷史記錄上下文跳轉到上一頁。navigate().forward()用於根據瀏覽器歷史記錄上下文跳轉到下一頁。示例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 BrowserNavigation {    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = ... 閱讀更多

什麼是 Selenium 中的 xpath?

Debomita Bhattacharjee
更新於 2020年6月10日 13:13:52

3K+ 次瀏覽

Xpath 是 Selenium 中用於識別 Web 元素的最重要的定位器之一。它的工作方式如下:它藉助元素及其屬性在文件物件模型 (DOM) 中導航以進行識別。雖然它有助於唯一地定位元素,但在速度方面比其他定位器慢。xpath 以兩種方式表示,即“/”和“//”。正斜槓表示絕對路徑。在這裡,xpath 直接從父級遍歷到 DOM 中的子級。因此,在絕對 xpath 中,我們必須從根節點遍歷到目標。語法 ... 閱讀更多

如何在 Selenium 中透過部分比較其屬性來識別元素?

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

1K+ 次瀏覽

我們可以使用正則表示式在 Selenium 中透過部分比較其屬性來識別元素。在 xpath 中,存在 contains() 方法。它支援屬性值的區域性匹配。在處理屬性中具有動態值的元素時,此方法非常有用。語法 driver.findElement(By.xpath("//tagname[contains(@attributes, ’value’)]"))在 CSS 中,我們可以使用正則表示式透過部分比較其屬性來識別元素。可能有三種情況:使用 ^ 來定位以特定文字開頭的屬性。語法 - driver.findElement(By.cssSelector("tagname[attribute^=’value’]"))使用 $ 來定位以特定文字結尾的屬性。語法 - driver.findElement(By.cssSelector("tagname[attribute$=’value’]"))使用 * 來定位包含特定文字的屬性。語法 - driver.findElement(By.cssSelector("tagname[attribute*=’value’]"))示例import org.openqa.selenium.By; ... 閱讀更多

如何在 Selenium 中根據頁面上可見的文字識別元素?

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

3K+ 次瀏覽

要根據頁面上可見的文字識別元素,請在 xpath 中使用 text() 方法。語法 - driver.findElement(By.xpath("//tagname[text()=’value’]"))示例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 TextMatch {    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().window().maximize();       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);       // 使用 text() 方法的 xpath       driver.findElement(By.xpath("//*[text()=’GATE Exams’]")).click();       driver.close();    } }閱讀更多

Selenium 支援哪些不同的定位器?

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

186 次瀏覽

Selenium 支援的各種定位器型別如下所示:
ID − 此屬性對每個元素都是唯一的。語法 − driver.findElement(By.id(""))。
Name − 此屬性並非對每個元素都是唯一的。語法 − driver.findElement(By.name(""))。
CSS 選擇器 − 此選擇器可以從元素標籤和屬性中派生。語法 − driver.findElement(By.cssSelector(""))。
XPath − 此選擇器可以從元素標籤和屬性中派生。語法 − driver.findElement(By.xpath(""))。
TagName − 此選擇器可以從 HTML 標籤中派生以識別元素。語法 − driver.findElement(By.tagName(""))。
LinkText − 此選擇器可以從錨文字中派生以識別元素。語法 − driver.findElement(By.linkText(""))。
PartialLinkText − 此選擇器可以從部分錨文字中派生以識別元素。語法 − driver.findElement(By.partialLinkText(""))。
Classname ... 閱讀更多

廣告