<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 9K WebDriver – 定位元素:第 2 部分(按`className`,`linkText`,`partialLinkText`) > 原文: [https://javabeginnerstutorial.com/selenium/9k-webdriver-locating-elements-2/](https://javabeginnerstutorial.com/selenium/9k-webdriver-locating-elements-2/) 朋友! 讓我們今天更深入地研究一下定位元素的更多策略。 在這篇文章中,我們將重點放在 * `className` * `LinkText` * `partialLinkText` ### 按類名稱定位 類名稱不過是用于設置 Web 元素樣式的 CSS 類名稱。 重要的是要注意頁面上的許多 Web 元素可能具有相同的`className`。 在這種情況下,可以使用`findElements`方法,并且可以為結果建立索引。 請參考通過`tagName`策略定位(解釋以及[先前文章](https://javabeginnerstutorial.com/selenium/9j-webdriver-locating-elements-1/)中提供的示例)。 如果我們有一個具有唯一`className`的元素,或者被測元素是該頁面中使用該`className`的第一個元素,則`findElement`將執行此任務。 **語法**:`driver.findElement(By.className("element_class_name"))`* **說明**:找到具有匹配 CSS 類名稱的第一個元素。 **示例**:讓我們找到 gmail 帳戶創建頁面的“手機”文本框。 右鍵單擊文本框,然后選擇檢查元素以獲取相應的 HTML 代碼。 我們可以看到“`input`”標簽包含`class ="i18n_phone_number_input-inner_input"`。 讓我們繼續使用此類名稱查找“手機”文本框,以進行進一步的交互。 *代碼:* ```java driver.findElement(By.className("i18n_phone_number_input-inner_input")); ``` ![Locating by ClassName](https://img.kancloud.cn/54/03/5403208027bb5db3781b5e4471a6d8bb_867x429.png) ### 通過`linkText`定位 當您想與超鏈接進行交互時,`linkText`非常有用。 使用該鏈接在網頁上顯示的實際文本。 那有多容易? **語法**:`driver.findElement(By.linkText("hyperlink_text");` **說明**:找到具有匹配鏈接文本的第一個超鏈接。 **示例**:讓我們找到在 gmail 帳戶創建頁面底部提供的超鏈接“了解更多信息”。 *代碼:* ```java driver.findElement(By.linkText("Learn more")); ``` ![Locating by LinkText](https://img.kancloud.cn/25/a8/25a84869d53a943c4e3b3a6911e81e6b_379x254.png) ### 通過`partialLinkText`定位 `PartialLinkText`也用于與超鏈接進行交互,與`linkText`定位策略非常相似。 此方法不提供部分完整鏈接,而是提供鏈接顯示的完整文本。 因此,可以將鏈接文本的一部分作為匹配條件。 **語法**:`driver.findElement(By.partialLinkText("hyperlink_partial_text");` **說明**:找到第一個超鏈接,其中**包含**指定的部分鏈接文本。 **示例**:讓我們在 Gmail 帳戶創建頁面的“選擇用戶名”文本框下方找到超鏈接“我更喜歡使用我當前的電子郵件地址” - 文本:**我更喜歡**。 *代碼:* ```java driver.findElement(By.partialLinkText("I prefer to")); ``` ![Locating by partialLinkText](https://img.kancloud.cn/f2/cf/f2cf29d45a9ec1e3a637833f4513157d_864x279.png) ## 概覽 讓我們看一個實現上述三種定位器類型的測試用例。 #### 場景 1. 打開 Firefox 瀏覽器。 2. 導航到 Google 帳戶創建頁面 3. 通過`className`找到手機文本框 4. 輸入“`9496543210`”作為手機號碼 5. 通過`linkText`找到“了解詳情”超鏈接 6. 鏈接超鏈接 7. 找到“我更喜歡使用當前的電子郵件地址”超鏈接和`partialLinkText` 8. 將完整的鏈接文本打印到控制臺進行驗證 此方案的 JUnit 代碼是, ```java 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 ElementLocatorTest2 { //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://accounts.google.com/SignUp"; } @Test public void testPageTitle() throws Exception{ // Open baseUrl in Firefox browser window driver.get(baseUrl); // Locate Mobile phone text box by className and // assign it to a variable of type WebElement WebElement mobileNum = driver.findElement(By.className("i18n_phone_number_input-inner_input")); // Clear the default placeholder or any value present mobileNum.clear(); // Enter/type the value to the text box mobileNum.sendKeys("9496543210"); // Locate 'Learn more' hyperlink by link text WebElement link1 = driver.findElement(By.linkText("Learn more")); // Click on 'Learn more' link1.click(); // Locate hyperlink by partial link text WebElement link2 = driver.findElement(By.partialLinkText("I prefer to")); // Printing the complete link text to console System.out.println("Complete link text: " + link2.getText()); } @After public void tearDown() throws Exception{ // Close the Firefox browser driver.close(); } } ``` *執行結果* 注釋清楚地提供給每一行代碼,因此很容易解釋。 ![Console output](https://img.kancloud.cn/20/61/20612c894c5fb8fe5b4adffd023ff451_849x163.png) 在 JUnit 窗格中,綠色條顯示測試用例已成功執行。 輸出將打印到控制臺,以確認僅提供子文本“我更喜歡”作為 partialLinkText,即可訪問“我更喜歡使用當前的電子郵件地址”超鏈接。 ![Output](https://img.kancloud.cn/04/38/04387f062be4b7ddd4ee742de15448a7_1128x305.png) 該圖像的左半部分顯示輸入的電話號碼,右半部分顯示在 Firefox 瀏覽器中執行的最終輸出。 點擊“了解詳情”鏈接后,我們將重定向到相應的頁面。 是時候再休息一次了。 在接下來的文章中,準備好消化更多的信息,因為我們將研究兩種有效的元素定位技術。 享受這一天!
                  <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>

                              哎呀哎呀视频在线观看