<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 9W WebDriver – 遍歷表元素 > 原文: [https://javabeginnerstutorial.com/selenium/9w-webdriver-looping-table-elements/](https://javabeginnerstutorial.com/selenium/9w-webdriver-looping-table-elements/) 歡迎回來,勇士們! 我們剛剛看到了如何檢查使用和不使用 XPath 的表的特定單元格中是否存在特定的[數據](https://javabeginnerstutorial.com/selenium/9v-webdriver-handling-tables-two-ways/)。 是時候遍歷每個元素并可視化 Selenium WebDriver 的功能了。 讓我們回到我們的[演示站點](https://chandanachaitanya.github.io/selenium-practice-site/)并關注“**書籍&作者”**表以了解這一概念。 ## 步驟 1: 使用其 ID “`BooksAuthorsTable`”找到“圖書&作者”表。 HTML 代碼如下: ```java <table id="BooksAuthorsTable" class="table table-bordered"> ``` *代碼:* ```java WebElement BooksTable = driver.findElement(By.id("BooksAuthorsTable")); ``` ## 步驟 2: **使用 XPath 計算行和列的總數**。 讓我們使用絕對 XPath 查找總行數。 請注意,XPath 以標簽名稱“`tr`”結尾。 `size()`方法給出使用 XPath 由`findElements()`返回的元素數。 *代碼:* ```java int rowNum = driver.findElements(By.xpath("/html/body/form/div[5]/div/div/table/tbody/tr")).size(); ``` 讓我們稍微切換一下齒輪,并使用相對 XPath 查找總列數。 *代碼:* ```java int colNum = driver.findElements(By.xpath("//table[@id='BooksAuthorsTable']/tbody/tr[1]/th")).size(); ``` 這種查找行和列總數的方法在表的行和列動態變化的情況下很有用。 ## 步驟 3: **遍歷表元素** 如果表的行和列號固定為,那么遍歷每個表元素將變得非常容易。 可以使用兩個`for()`循環,一個用于行,另一個用于訪問列值。 *代碼:* ```java for(int i=2; i<=6; i++){ for(int j=1; j<=4; j++){ System.out.print(driver.findElement(By. xpath("//table[@id='BooksAuthorsTable']/tbody/tr[" + i +"]/td[" + j + "]")).getText() + "\t"); } System.out.println(""); } ``` 有一天,如果幸運的話,您可能會偶然發現一個表,該表在每次頁面刷新時都會動態加載,因此行數通常會有所不同。 因此,每執行一次測試便要計算行數。 首先,讓我們獲取所有標簽名稱為“”的元素,并將它們放在名為“`rowVals`”的列表中。 ```java List<WebElement> rowVals = BooksTable.findElements(By.tagName("tr")); ``` 要從第一行獲取標頭元素,請找到所有標簽名稱為“`th`”的元素,并將其放在名為“`colHeader`”的列表中。 ```java List<WebElement> colHeader = rowVals.get(0).findElements(By.tagName("th")); ``` 要將每個標題文本打印到控制臺,請遍歷`colHeader`列表中的標題值并使用`getText()`方法。 ```java for(int i=0; i<colHeader.size(); i++){ System.out.println(colHeader.get(i).getText()); } ``` 要將表內容打印到控制臺,請遍歷每一行。 對于每一行,遍歷每一列,并使用相同的`getText()`方法打印值。 ```java for(int i=1; i<rowNum; i++){ List<WebElement> colVals = rowVals.get(i).findElements(By.tagName("td")); for(int j=0; j<colNum; j++){ System.out.println(colVals.get(j).getText()); } } ``` 您有能力集中精力下坡嗎? 如果是,讓我們看看 BrainBell! **BrainBell – 可視化!** *我們大多數人都知道,為我們希望記住的概念創建心理圖片比單詞更清晰,更容易記住。 我們不知道或做的是仔細觀察它幾秒鐘。 這將有助于輕松調用它。* 話雖如此,讓我們看一下到目前為止討論的整體情況! ### 場景 1. 打開 Firefox 瀏覽器 2. 導航到[演示站點](https://chandanachaitanya.github.io/selenium-practice-site/) 3. 使用 ID 定位“圖書&作者”表 4. 使用絕對 XPath 獲取總行數 5. 使用相對 XPath 獲取總列數 6. 將行數和列數打印到控制臺 7. 通過標簽名稱“`tr`”獲取所有行值 8. 通過標簽名稱“`th`”獲取列表中的列標題值 9. 循環瀏覽標題值并將其打印到控制臺 10. 遍歷表內容(每一行的所有列)并獲取其文本 11. 將值打印到控制臺 12. 使用固定的行號和列號將表內容打印到控制臺 此方案的 JUnit 代碼是, ```java package com.blog.junitTests; import java.util.List; 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 LoopingThroughTableElements { // 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 'Books & Authors' table using id WebElement BooksTable = driver.findElement(By.id("BooksAuthorsTable")); //Get all web elements by tag name 'tr' List<WebElement> rowVals = BooksTable.findElements(By.tagName("tr")); //Get number of rows and columns //using absoulute xpath int rowNum = driver.findElements(By.xpath("/html/body/form/div[5]/div/div/table/tbody/tr")).size(); //using relative xpath int colNum = driver.findElements(By.xpath("//table[@id='BooksAuthorsTable']/tbody/tr[1]/th")).size(); System.out.println("Total number of rows = " + rowNum); System.out.println("Total number of columns = " + colNum); //Get column header values from first row List<WebElement> colHeader = rowVals.get(0).findElements(By.tagName("th")); //Loop through the header values and print them to console System.out.println("Header values:"); for(int i=0; i<colHeader.size(); i++){ System.out.println(colHeader.get(i).getText()); } System.out.println("---------------"); //Loop through the remaining rows for(int i=1; i<rowNum; i++){ //Get each row's column values by tag name List<WebElement> colVals = rowVals.get(i).findElements(By.tagName("td")); //Loop through each column for(int j=0; j<colNum; j++){ //Print the coulumn values to console System.out.println(colVals.get(j).getText()); } //Just a separator for each row System.out.println("---------------"); } //Printing table contents to console for fixed row and column numbers for(int i=2; i<=6; i++){ for(int j=1; j<=4; j++){ System.out.print(driver.findElement(By. xpath("//table[@id='BooksAuthorsTable']/tbody/tr[" + i +"]/td[" + j + "]")).getText() + "\t"); } System.out.println(""); } } //End of @Test @After public void tearDown() throws Exception { // Close the Firefox browser driver.close(); } } ``` ### 執行結果: 到目前為止,每行代碼都作為概念的一部分進行了很好的解釋。 注意到 Eclipse IDE 的 JUnit 視圖后,綠色條顯示測試用例已成功執行。 ![Table looping eclipse output](https://img.kancloud.cn/15/39/153926e2d7999a4556cfcafb2d04fd38_686x167.png) 控制臺窗口顯示沒有任何錯誤。 它還按預期顯示固定和動態計算的行數和列數的打印表內容。 ![Table looping console output](https://img.kancloud.cn/c9/62/c962b56cd0566ebe5b7e3b8816662683_882x566.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>

                              哎呀哎呀视频在线观看