<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 9V WebDriver – 以兩種方式處理表 > 原文: [https://javabeginnerstutorial.com/selenium/9v-webdriver-handling-tables-two-ways/](https://javabeginnerstutorial.com/selenium/9v-webdriver-handling-tables-two-ways/) 嗨呀冠軍! 歡迎回到關于使用 Selenium WebDriver 處理 Web 元素的另一篇文章。 您每天都會學到一些新知識,而這就是我們今天將要學習的內容:使用和不使用 XPath 處理表! 讓我們回到我們的[演示站點](https://chandanachaitanya.github.io/selenium-practice-site/)以更好地理解這個概念。 餐桌就像到處都是名人。 好吧,是時候該去見我們的明星了! 在整個這篇文章中,我們將使用演示站點中的“實踐表”表。 在很多情況下,您可能需要檢查表的特定單元格中是否存在特定數據。 因此,讓我們繼續前進,擊中頭部的粗體! ## 方法 1:不使用 XPath 處理表 在“練習表”中,我們有一個具有兩行兩列的表。 我們在第一行第二列中嵌套了另一個表。 **例如**: 讓我們嘗試訪問嵌套表的第一行,并將文本“`Inner Row 1`”打印到控制臺。 由于我們不在這里使用 XPath,因此它將變得有些復雜。 但是一旦您掌握了如何以一個嵌套級別定位一個單元,然后訪問更深層次的單元就輕松了! 第一步是使用標簽名稱“`table`”查找主表。 可以使用任何定位策略,但通常表的行和列沒有 ID 或名稱。 因此,訪問它們的最佳方法是使用它們相應的標簽名稱。 第二步是使用獲得的 Web 元素(表),找到嵌套表。 最后一步是,使用嵌套表 web 元素,找到第一行并獲取其文本! 右鍵單擊所需元素,然后選擇檢查元素,將給出相應的 HTML 代碼段,如下所示, ```java <table class="table table-bordered"> <tr> <td>Row 1</td> <td> <table class="table table-bordered"> <tr> <td>Inner Row 1</td> </tr> <tr> <td>Inner Row 2</td> </tr> </table> ``` *代碼:* ```java // Locate 'Table For Practice' using tagName WebElement practiceTable = driver.findElement(By.tagName("table")); // Locate Inner table using tagName WebElement innerTable = practiceTable.findElement(By.tagName("table")); // Locate 'Inner Row 1' using tagName WebElement innerRow1 = innerTable.findElement(By.tagName("td")); // Print the first row text to console System.out.println("Inner table first row text = " + innerRow1.getText()); ``` ## 方法 2:使用 XPath 處理表 第一種方法需要使用三個 Web 元素來訪問所需的單元格。 如果涉及許多嵌套級別,則用于定位元素的命令數量將增加。 為了減輕這種情況,我們有 XPath 可以拯救我們! “XPath”一詞會響起嗎? 如果不是這樣,請參考 XPath 的[定位元素以簡單的方式構造它](https://javabeginnerstutorial.com/selenium/9n-webdriver-locating-elements-4a/)。 **例如**: 讓我們找到嵌套表的第二行,并將其內容打印到控制臺。 相對路徑是 XPath 的一部分,用于定位嵌套表的所需單元格。 *代碼:* ```java // Locate 'Inner Row 2' using xPath WebElement innerRow2 = driver.findElement(By.xpath("//table/tbody/tr[1]/td[2]/table/tbody/tr[2]/td[1]")); ``` ## 概覽 讓我們一步一步地查看上面討論的方法的代碼! *場景* 1. 打開 Firefox 瀏覽器 2. 導航到演示站點( [https://chandanachaitanya.github.io/selenium-practice-site/](https://chandanachaitanya.github.io/selenium-practice-site/) ) 3. 使用標簽名稱找到“練習表格” 4. 使用標簽名稱找到嵌套表 5. 使用標簽名稱找到嵌套表的第一行 6. 打印文本“`Inner Row 1`”進行控制臺 7. 使用 XPath 找到嵌套表的第二行 8. 打印文本“`Inner Row 2`”進行控制臺 此方案的 JUnit 代碼是, ```java package com.blog.junitTests; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class HandlingTables { // 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(); // 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); // Locate 'Table For Practice' using tagName WebElement practiceTable = driver.findElement(By.tagName("table")); // Locate Inner table using tagName WebElement innerTable = practiceTable.findElement(By.tagName("table")); // Locate 'Inner Row 1' using tagName WebElement innerRow1 = innerTable.findElement(By.tagName("td")); // Print the first row text to console System.out.println("Inner table first row text = " + innerRow1.getText()); // Locate 'Inner Row 2' using xPath WebElement innerRow2 = driver.findElement(By.xpath("//table/tbody/tr[1]/td[2]/table/tbody/tr[2]/td[1]")); System.out.println("Inner table second row text = " + innerRow2.getText()); } //End of @Test @After public void tearDown() throws Exception { // Close the Firefox browser driver.close(); } } ``` *執行結果:* 每行代碼都帶有不言自明的注釋,并且作為到目前為止涵蓋的概念的一部分,代碼得到了很好的解釋。 注意到 Eclipse IDE 的 JUnit 視圖后,綠色條顯示測試用例已成功執行。 控制臺窗口顯示沒有任何錯誤。 它還按預期顯示了兩個嵌套表行的文本。 ![Handling tables eclipse output](https://img.kancloud.cn/68/77/687713c99cc265a9ca6525a873b8253b_704x325.png) 任何問題? 在評論部分開火! 稍后再見。 祝你今天愉快!
                  <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>

                              哎呀哎呀视频在线观看