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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 如何在 Selenium WebDriver 中處理 Web 表 > 原文: [https://www.guru99.com/selenium-webtable.html](https://www.guru99.com/selenium-webtable.html) ## 讀取 HTML Web 表 有時我們需要訪問 HTML 表中的元素(通常是文本)。 但是,對于 Web 設計人員來說,很少向表中的某個單元格提供 id 或 name 屬性。 因此,我們不能使用“ By.id()”,“ By.name()”或“ By.cssSelector()”之類的常用方法。 在這種情況下,最可靠的選擇是使用“ By.xpath()”方法訪問它們。 在本教程中,您將學習- * [如何為表](#10)編寫 XPath * [訪問嵌套表](#11) * [使用屬性作為謂詞](#12) * [快捷方式:使用檢查元素訪問硒](#13)中的表 ## 如何為表編寫 XPath 考慮下面的 HTML 代碼。 ![](https://img.kancloud.cn/7a/11/7a115085a55c49c36b1db1306665a063_349x348.png).png) 我們將使用 [XPath](/xpath-selenium.html) 獲取包含文本“第四個單元格”的單元格的內部文本。 ![](https://img.kancloud.cn/37/cd/37cd3f2a8dacd359d2ecfe77fcb8912f_213x224.png).png) **步驟 1-設置父元素(表格)** **WebDriver 中的 XPath 定位符始終以雙斜杠“ //”開頭,然后是父元素**。 由于我們正在處理表,因此父元素應始終為<表>標簽。 因此,我們的 XPath 定位器的第一部分應以“ // table”開頭。 ![](https://img.kancloud.cn/48/a1/48a106bae59aadb8e44a6fbc31126b45_146x37.png).png) **步驟 2-添加子元素** <表>緊鄰的元素是< tbody >,因此我們可以說< tbody >是<表>的“子”。 而且,<表>是< tbody >的“父”。 XPath 中的所有子元素都放置在其父元素的右邊,并用一個正斜杠“ /”分隔,如下所示。 ![](https://img.kancloud.cn/73/0a/730a823ed9eb94604f8744787d01ce23_195x208.png) ](/images/image022(1).png) [ ![](https://img.kancloud.cn/e0/43/e04356bfd5513eb63e6fda0e6b084c43_205x145.png) **步驟 3-添加謂詞** <正文>元素包含兩個< tr >標簽。 現在我們可以說這兩個< tr >標簽是< tbody >的“子代”。 因此,可以說< tbody >是< tr >元素的父代。 我們可以得出的另一結論是,兩個< tr >元素是同級元素。 **兄弟姐妹是指具有相同父項**的子元素。 要訪問我們要訪問的< td >(文本為“第四單元格”的那個),我們必須首先訪問**第二個** < tr >,而不是第一個 。 如果我們只寫“ // table / tbody / tr”,那么我們將訪問第一個< tr >標簽。 那么,我們如何訪問第二個< tr >呢? 答案是使用**謂詞**。 **謂詞是用方括號“ []”括起來的數字或 HTML 屬性,用于將子元素與其同級元素**區別開。 由于我們需要訪問的< tr >是第二個,因此我們將使用“ [2]”作為謂詞。 ![](https://img.kancloud.cn/66/15/66151c07719780bac710d778df4276c4_242x189.png) 如果我們不使用任何謂詞,那么 XPath 將訪問第一個兄弟。 因此,我們可以使用這兩個 XPath 代碼之一訪問第一個< tr >。 ![](https://img.kancloud.cn/17/89/1789fb88c0e2bcfd9c09d5433fb9c302_535x218.png) **步驟 4-使用適當的謂詞**添加成功的子元素 我們需要訪問的下一個元素是第二個< td >。 運用從第 2 步和第 3 步中學到的原理,我們將最終確定 XPath 代碼,如下所示。 ![](https://img.kancloud.cn/a3/2e/a32e555b1946e89d4eeb30bf8ea32703_227x42.png) 現在我們有了正確的 XPath 定位器,我們已經可以使用下面的代碼訪問所需的單元格并獲取其內部文本。 假定您已將上述 HTML 代碼保存為 C 驅動器中的“ newhtml.html”。 ![](https://img.kancloud.cn/79/46/7946345b0ea5e014f00c8a8d305ec186_466x176.png) ``` public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/write-xpath-table.html"; WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); String innerText = driver.findElement( By.xpath("//table/tbody/tr[2]/td[2]")).getText(); System.out.println(innerText); driver.quit(); } } ``` ![](https://img.kancloud.cn/c1/8f/c18f645e25cb265d73504784310a5204_258x220.png) ## 訪問嵌套表 上面討論的相同原理適用于嵌套表。 **嵌套表是位于另一個表**中的表。 一個例子如下所示。 ![](https://img.kancloud.cn/cb/d8/cbd8a15a3973cd83a3e18577bdd5ea87_403x563.png) ](/images/image029.png) [ ![](https://img.kancloud.cn/a2/88/a288e9299dce6d0ac3079f8440d3fd11_245x190.png) 要使用上一節中的“ // parent / child”和謂詞概念訪問帶有文本“ 4-5-6”的單元格,我們應該能夠在下面提出 XPath 代碼。 ![](https://img.kancloud.cn/32/6f/326f4ddd3f1f30b00758af5cd7449e77_394x143.png) 下面的 WebDriver 代碼應該能夠檢索我們正在訪問的單元格的內部文本。 ![](https://img.kancloud.cn/12/77/12779d2576584b214836e059f752dfdc_491x181.png) ``` public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/accessing-nested-table.html"; WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); String innerText = driver.findElement( By.xpath("//table/tbody/tr[2]/td[2]/table/tbody/tr/td[2]")).getText(); System.out.println(innerText); driver.quit(); } ``` 以下輸出確認內部表已成功訪問。 ![](https://img.kancloud.cn/29/d2/29d26b9c26410bbd827ec2bd9198050c_239x172.png) ## 使用屬性作為謂詞 如果該元素是用 HTML 代碼深層編寫的,則謂詞所用的數字很難確定,那么我們可以改用該元素的唯一屬性。 在下面的示例中,“紐約到芝加哥”單元格位于 Mercury Tours 主頁的 HTML 代碼的深處。 ![](https://img.kancloud.cn/8e/f6/8ef6c39d0c63930f8adfec8ef7619f6d_282x159.png) ![](https://img.kancloud.cn/c8/fa/c8fa25b73a6acc3a035dd31b273df702_779x639.png) 在這種情況下,我們可以使用表的唯一屬性(width =“ 270”)作為謂詞。 **通過在屬性前面加上@符號**來用作謂詞。 在上面的示例中,“紐約到芝加哥”單元格位于第四< tr >的第一< td >中,因此我們的 XPath 應該如下所示。 ![](https://img.kancloud.cn/6d/84/6d8413efe08050733245fdf962217201_319x37.png) 請記住,當我們將 XPath 代碼放入 Java 中時,應在“ 270”的兩邊使用轉義字符反斜杠“ \”作為雙引號,以便 By.xpath()的字符串參數不會過早終止。 。 ![](https://img.kancloud.cn/c2/54/c254fac4e698a34a36813eca70d31386_416x117.png) 現在,我們準備使用下面的代碼訪問該單元格。 ![](https://img.kancloud.cn/03/57/0357bd4684ae30a3c08e8b35a1ec4604_467x191.png) ``` public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/newtours/"; WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); String innerText = driver.findElement(By .xpath("//table[@width=\"270\"]/tbody/tr[4]/td")) .getText(); System.out.println(innerText); driver.quit(); } ``` ![](https://img.kancloud.cn/80/27/802722fa220fa90ce256022efd3db361_259x176.png) ## 快捷方式:使用檢查元素訪問硒中的表 如果元素的編號或屬性非常困難或無法獲得,則生成 XPath 代碼的最快方法是使用 Inspect Element。 請考慮以下 Mercury Tours 主頁上的示例。 ![](https://img.kancloud.cn/33/8b/338b8aa756f6682375936c925e735531_297x232.png) **步驟 1** 使用 Firebug 獲取 XPath 代碼。 ![](https://img.kancloud.cn/c8/7f/c87f097ce93a1bc609ada4b222629224_578x533.png) **步驟 2** 查找第一個“表”父元素,然后刪除其左側的所有內容。 ![](https://img.kancloud.cn/85/42/8542e95d0c5161668e9fff1580abf3c8_392x139.png) **步驟 3** 在代碼的其余部分前面加上雙斜杠“ //”,并將其復制到 WebDriver 代碼中。 ![](https://img.kancloud.cn/f1/94/f194b0a72e2ab5ffd364649f9474f9c1_406x329.png) 下面的 WebDriver 代碼將能夠成功檢索我們正在訪問的元素的內部文本。 ![](https://img.kancloud.cn/10/b8/10b8d145b3748d01bab8429d790b1c65_395x441.png) ``` public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/newtours/"; WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); String innerText = driver.findElement(By .xpath("//table/tbody/tr/td[2]" + "//table/tbody/tr[4]/td/" + "table/tbody/tr/td[2]/" + "table/tbody/tr[2]/td[1]/" + "table[2]/tbody/tr[3]/td[2]/font")) .getText(); System.out.println(innerText); driver.quit(); } ``` **摘要** * By.xpath()通常用于訪問表元素。 * 如果該元素是用 HTML 代碼深層編寫的,則謂詞所用的數字很難確定,那么我們可以改用該元素的唯一屬性。 * 通過在屬性前面加上@符號來將其用作謂詞。 * 使用 Inspect 元素訪問 Selenium 中的表
                  <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>

                              哎呀哎呀视频在线观看