<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之旅 廣告
                # 在 Selenium Webdriver 中按鏈接文本&部分鏈接文本定位元素 > 原文: [https://www.guru99.com/locate-by-link-text-partial-link-text.html](https://www.guru99.com/locate-by-link-text-partial-link-text.html) **符合條件的鏈接** 可以使用鏈接文本的完全匹配或部分匹配來訪問鏈接。 下面的示例提供了多個匹配項存在的場景,并說明了 WebDriver 如何處理它們。 在本教程中,我們將學習使用 Webdriver 查找和訪問鏈接的可用方法。 另外,我們將討論訪問鏈接時遇到的一些常見問題,并將進一步討論如何解決它們。 這是您將學到的- * [使用精確文本匹配訪問鏈接:By.linkText()](#2) * [使用部分文本匹配訪問鏈接:By.partialLinkText()](#3) * [如何獲取具有相同鏈接文本的多個鏈接](#5) * [鏈接文本](#4)區分大小寫 * [鏈接到塊的內部和外部](#6) ## 使用精確文本匹配訪問鏈接:By.linkText() **通過 By.linkText()方法**使用鏈接的確切鏈接文本訪問鏈接。 但是,如果有兩個鏈接具有完全相同的鏈接文本,則此方法將僅訪問第一個。 考慮下面的 HTML 代碼 ![](https://img.kancloud.cn/5e/ae/5eae19cf4dccccaeb6be3e70b062c466_443x187.png) ![](https://img.kancloud.cn/be/92/be926e9332d8fb64992bfb966b45c4b0_386x154.png).png) 當您嘗試運行下面的 WebDriver 代碼時,您將訪問第一個“單擊此處”鏈接 ![](https://img.kancloud.cn/35/b9/35b9d24895784241fa6e2d82d18c5d39_467x154.png).png) **代碼:** ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class MyClass { public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/link.html"; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); driver.findElement(By.linkText("click here")).click(); System.out.println("title of page is: " + driver.getTitle()); driver.quit(); } } ``` 下面是它的工作原理- ![](https://img.kancloud.cn/e2/1d/e21d4119d80b2624c8975d91adb5a767_575x162.png) 因此,您將被自動帶到 Google。 ![](https://img.kancloud.cn/d2/63/d263acb0744798187a0a172ebde06afd_265x109.png).png) ## 使用部分文本匹配訪問鏈接:By.partialLinkText() 使用 **By.partialLinkText()**方法來完成使用一部分鏈接文本訪問鏈接的操作。 如果您指定具有多個匹配項的部分鏈接文本,則僅第一個匹配項將被訪問。 考慮下面的 HTML 代碼。 ![](https://img.kancloud.cn/15/18/1518087ec9cb73f5f056ffd8929a8062_429x188.png).png) ![](https://img.kancloud.cn/00/9c/009cb8dc20c07ab3ab63e05fb008b08a_228x162.png).png) 當您執行以下 WebDriver 代碼時,您仍將被帶到 Google。 ![](https://img.kancloud.cn/54/2d/542d38ceeaf036e74168c45e5fde3a63_476x152.png).png) **Code:** ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class P1 { public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/accessing-link.html"; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); driver.findElement(By.partialLinkText("here")).click(); System.out.println("Title of page is: " + driver.getTitle()); driver.quit(); } } ``` ![](https://img.kancloud.cn/1b/c7/1bc70e3bd3ea5ee01876e8cf6a578339_245x109.png).png) ## 如何使用相同的鏈接文本獲取多個鏈接 那么,如何解決以上問題呢? 如果存在多個具有相同鏈接文本的鏈接,并且我們要訪問第一個鏈接以外的鏈接,我們該如何處理? 在這種情況下,通常使用不同的定位符,即... By.xpath(),By.cssSelector()或 By.tagName()。 最常用的是 By.xpath()。 它是最可靠的一種,但看起來也很復雜且不可讀。 ## 鏈接文本區分大小寫 ![](https://img.kancloud.cn/44/51/4451358d11334cd03b4778e6c7e5b2ce_472x261.png) **By.linkText()**和 **By.partialLinkText()**的參數均區分大小寫,這意味著大寫很重要。 例如,在 Mercury Tours 的主頁中,有兩個鏈接包含文本“ egis”-一個是在頂部菜單中找到的“ REGISTER”鏈接,另一個是在右下部分中找到的“ Register here”鏈接 頁面。 ![](https://img.kancloud.cn/ed/80/ed80d008f299c3de2b6a7e52a41b0193_457x198.png).png) 盡管兩個鏈接都包含字符序列“ egis”,但其中一個是“ By.partialLinkText()”方法將根據字符的大寫字母分別訪問這兩個鏈接。 請參見下面的示例代碼。 ![](https://img.kancloud.cn/b7/00/b7008c37e59f072aaf2f01c7cc33a50b_566x279.png).png) **代碼** ``` public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/newtours/"; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); String theLinkText = driver.findElement(By .partialLinkText("egis")) .getText(); System.out.println(theLinkText); theLinkText = driver.findElement(By .partialLinkText("EGIS")) .getText(); System.out.println(theLinkText); driver.quit(); } ``` ## 鏈接到塊的內部和外部 最新的 HTML5 標準允許<和>標簽放置在塊級標簽的內部和外部,例如< div >,< p >或< h3 > 。 “ By.linkText()”和“ By.partialLinkText()”方法可以訪問位于這些塊級元素外部和內部的鏈接。 考慮下面的 HTML 代碼。 ![](https://img.kancloud.cn/6e/d9/6ed9fdb24ebda15b363e3d849e9b70c9_572x219.png).png) ![](https://img.kancloud.cn/cf/4c/cf4c57de601dced490c9cd3ea84cb92a_228x231.png).png) 下面的 WebDriver 代碼使用 By.partialLinkText()方法訪問這兩個鏈接。 ![](https://img.kancloud.cn/2f/eb/2febd9c9162f1c5bc286127ae8876d43_589x345.png).png) **Code:** ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class MyClass { public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/block.html"; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); driver.findElement(By.partialLinkText("Inside")).click(); System.out.println(driver.getTitle()); driver.navigate().back(); driver.findElement(By.partialLinkText("Outside")).click(); System.out.println(driver.getTitle()); driver.quit(); } } ``` 上面的輸出確認兩個鏈接均已成功訪問,因為正確檢索了它們各自的頁面標題。 ### 摘要 * 使用 click()方法訪問鏈接。 * 除了適用于任何 WebElement 的定位器之外,鏈接還具有基于鏈接文本的定位器: * By.linkText()–根據作為參數提供的鏈接文本的精確匹配來定位鏈接。 * By.partialLinkText()–根據鏈接文本的部分文本匹配找到鏈接。 * 以上兩個定位符均區分大小寫。 * 如果存在多個匹配項,By.linkText()和 By.partialLinkText()將僅選擇第一個匹配項。 在存在具有相同鏈接文本的多個鏈接的情況下,將使用基于 xpath,CSS 的其他定位符。 * findElements()& By.tagName(“ a”)方法查找頁面中與定位器條件匹配的所有元素 * 鏈接可以通過 By.linkText()和 By.partialLinkText()進行訪問,無論它們位于塊級元素之內還是之外。
                  <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>

                              哎呀哎呀视频在线观看