<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 9AA WebDriver – 執行 JavaScript 代碼 > 原文: [https://javabeginnerstutorial.com/selenium/9aa-webdriver-executing-javascript-code/](https://javabeginnerstutorial.com/selenium/9aa-webdriver-executing-javascript-code/) 嗨冠軍! 今天就與瀏覽器進行一些高質量的互動!! 因此,您能猜出瀏覽器首選的語言嗎? 是的,你說對了。 確實是 **JavaScript** 。 如果您使用的是 Chrome,則單擊“F12”將打開“開發人員工具”,這有助于我們直接從瀏覽器執行 JavaScript。 某些動作(例如滾動,顯示警報等)變得更易于使用 JavaScript 進行管理。 ![Executing JavaScript in browser](https://img.kancloud.cn/77/bf/77bf7160c085237e1730e5ec7c5247c5_791x669.png) 除此之外,在其他情況下,我們可能找不到合適的 Selenium API 來執行特定操作。 但是我們可以通過執行 JavaScript 代碼來執行該操作。 Selenium WebDriver 提供了一個界面,可以幫助我們做到這一點! `JavascriptExecutor`…您聽說過,就像那些名人一樣。 好吧,現在是我們該去見那顆璀璨的星星的時候了。 `JavascriptExecutor`不需要添加任何外部 JAR 文件或插件。 只需導入一個包即可完成工作,“導入`org.openqa.selenium.JavascriptExecutor`”。 它有兩種重要的方法可以執行我們的 Selenium 測試中的 JavaScript 代碼,以自動化測試中的應用,即 * **`executeScript(script, args)`** * **`executeAsyncScript(script, args)`** ![JavascriptExecutor methods](https://img.kancloud.cn/80/4d/804d4c784429cd0b620fc90afa53b994_511x130.png) 讓我們通過幾個簡單的步驟來理解這一點。 ## 步驟 1: 導入以下包,`import org.openqa.selenium.JavascriptExecutor;` 創建一個`JavascriptExecutor`對象,并通過將其類型轉換為`JavascriptExecutor`來分配驅動程序對象。 ```java // Typecast driver to JavascriptExecutor JavascriptExecutor jsExecutor = (JavascriptExecutor)driver; ``` ## 步驟 2: 這樣創建的`JavascriptExecutor`對象允許我們從 Selenium 測試中執行 JavaScript 代碼。 ```java // Scroll down by 100 pixels jsExecutor.executeScript("window.scrollBy(0,100)"); ``` 此“`excuteScript`”方法采用兩個參數。 第一個是 JavaScript 代碼,第二個是 Java 腳本代碼所需的可選參數列表。 ## 概覽 讓我們看一個測試案例,實現到目前為止所介紹的技術, ### 場景 1. 打開 Firefox 瀏覽器 2. 導航到演示站點([https://chandanachaitanya.github.io/selenium-practice-site/](https://chandanachaitanya.github.io/selenium-practice-site/)) 3. 使用和不使用 JavaScript 代碼打印頁面 URL 進行控制臺 4. 垂直向下滾動頁面 100 像素 5. 刷新頁面 6. 導航到 Google 主頁 7. 使用 JavaScript 代碼執行上述三個操作 8. 驗證 Eclipse IDE 控制臺輸出屏幕和 JUnit 窗格是否成功 此方案的 JUnit 代碼是, ```java import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class JavaScriptExecutorTest { // Declaring variables private WebDriver driver; private String baseUrl; @Before public void setUp() throws Exception { // Selenium version 3 beta releases require system property set up System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\" + "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe"); // Create a new instance for the class FirefoxDriver // that implements WebDriver interface driver = new FirefoxDriver();53 // Implicit wait for 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Assign the URL to be invoked to a String variable baseUrl = "https://chandanachaitanya.github.io/selenium-practice-site/"; } @Test public void testPageTitle() throws Exception { // Open baseUrl in Firefox browser window driver.get(baseUrl); Thread.sleep(5000); // Typecast driver to JavascriptExecutor JavascriptExecutor jsExecutor = (JavascriptExecutor)driver; // Execute JavaScript code and assign it to a String String pageURL = (String)jsExecutor.executeScript("return document.URL;"); // Print the URL to the console System.out.println("URL : " + pageURL); // Print the URL without JavaScript to the console System.out.println("URL without JavascriptExecutor: " + driver.getCurrentUrl()); // Scroll down by 100 pixels jsExecutor.executeScript("window.scrollBy(0,100)"); // Refresh the page jsExecutor.executeScript("history.go(0)"); // Navigating to a different page jsExecutor.executeScript("window.location = 'https://www.google.com/';"); } // End of @Test @After public void tearDown() throws Exception { // Close the Firefox browser driver.close(); } } ``` ### 執行結果: 為每行代碼提供了注釋,使其易于說明。 在 JUnit 窗格中,綠色條顯示測試用例已成功執行。 控制臺窗口顯示沒有任何錯誤。 它還按預期顯示所有打印的消息。 ![Eclipse IDE console output](https://img.kancloud.cn/a0/93/a0932b6089b27f315e49fded203ffdd9_813x259.png) 是時候嘗試今天的技能了。 是的,戴上安全帽,以免遇到任何異常! 所有代碼文件都放置在 [GitHub 倉庫](https://github.com/JBTAdmin/Selenium)中,以方便訪問。 您可以為倉庫加注星標和分支以方便使用。 請仔細閱讀“`README.md`”文件以獲取明確說明。 祝你有美好的一天!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看