<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # XPath 在 Selenium Webdriver 中包含,同級,祖先,與或,父級,開頭,軸 > 原文: [https://www.guru99.com/using-contains-sbiling-ancestor-to-find-element-in-selenium.html](https://www.guru99.com/using-contains-sbiling-ancestor-to-find-element-in-selenium.html) 如果簡單的 [XPath](/xpath-selenium.html) 不能為我們的測試腳本找到復雜的 Web 元素,則需要使用 XPath 1.0 庫中的函數。 通過這些功能的組合,我們可以創建更特定的 XPath。 讓我們討論 3 種這樣的功能– 1. [包含](#1) 2. [兄弟姐妹](#2) 3. [祖先](#3) 4. [和或](#4) 5. [父級](#5) 6. [以](#6)開頭 7. [XPath 軸](#7) 讓我們詳細研究它們- ## 包含 通過在 XPath 中使用“包含”功能,我們可以提取與特定文本值匹配的所有元素。 例如 在這里,我們正在搜索錨點,其中包含的文本為“ SAP M”。 ``` "//h4/a[contains(text(),'SAP M')]" ``` ![](https://img.kancloud.cn/0d/fc/0dfc5af93e4e3403d946a55159a11ec3_580x260.png) ## 兄弟姐妹 使用同級關鍵字,我們可以在上獲取與其他元素相關的 Web 元素。 示例:在此基于“ a”的同級元素,我們找到“ h4” ``` "//div[@class='canvas- graph']//a[@href='/accounting.html'][i[@class='icon-usd']]/following-sibling::h4" ``` ![](https://img.kancloud.cn/ca/71/ca7172634d728aeef1c18451440aff36_581x201.png) **祖先**:要在父元素的基礎上查找元素,我們可以使用 XPath 的祖先屬性。 ![](https://img.kancloud.cn/53/9e/539e37d49c0642fd53af1ad1bf88763b_624x148.png) 讓我們通過一個例子來理解這三個功能– 測試步驟 **注意:**自創建教程以來,Guru99 的主頁已更新,因此請使用演示站點來運行測試。 1. 轉到 [http://demo.guru99.com/test/guru99home/](http://demo.guru99.com/test/guru99home/) 2. 在“一些最受歡迎的課程”部分中,搜索與文本為“ SELENIUM”的 WebElement 的同級的所有 Web 元素。 3. 我們將使用 contains,祖先和同級函數找到元素 ![](https://img.kancloud.cn/c9/c8/c9c84692ee524021ded6fe4fdd976926_624x212.png) ## 使用包含和同級 ``` import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class SiblingAndParentInXpath { @Test public void testSiblingAndParentInXpath(){ WebDriver driver; String driverPath = "C:\\geckodriver.exe"; System.setProperty("webdriver.gecko.driver", driverPath); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://demo.guru99.com/test/guru99home/"); //Search element inside 'Popular course' which are sibling of control 'SELENIUM' ,Here first we will find a h2 whose text is ''A few of our most popular courses' ,then we move to its parent element which is a 'div' , inside this div we will find a link whose text is 'SELENIUM' then at last we will find all of the sibling elements of this link('SELENIUM') List <WebElement> dateBox = driver.findElements(By.xpath("//h2[contains(text(),'A few of our most popular courses')]/parent::div//div[//a[text()='SELENIUM']]/following-sibling::div[@class='rt-grid-2 rt-omega']")); //Print all the which are sibling of the the element named as 'SELENIUM' in 'Popular course' for (WebElement webElement : dateBox) { System.out.println(webElement.getText()); } driver.close(); } } ``` 輸出將如下所示: ![](https://img.kancloud.cn/e0/7e/e07e2bee4cc372fae6a136368b78a876_423x247.png) ## 祖先功能 我們也可以借助“祖先”功能實現相同的功能。 現在,假設我們需要在文本為“ SELENIUM”的錨的祖先的幫助下搜索“熱門課程”部分中的所有元素 在這里,我們的 xpath 查詢將像 ``` "//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div" ``` ### 完整的代碼 ``` import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class AncestorInXpath{ @Test public void testAncestorInXpath(){ WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://demo.guru99.com/test/guru99home/"); //Search All elements in 'Popular course' section //with the help of ancestor of the anchor whose text is 'SELENIUM' List <WebElement> dateBox = driver.findElements(By.xpath("//div[.//a[text()='SELENIUM']]/ancestor::div[@class='rt-grid-2 rt-omega']/following-sibling::div")); //Print all the which are sibling of the element named as 'SELENIUM' in 'Popular course' for (WebElement webElement : dateBox) { System.out.println(webElement.getText()); } driver.quit(); } } ``` 輸出看起來像- ![](https://img.kancloud.cn/16/74/16746955a574b7057386ec06f9febec2_470x207.png) ## 使用 AND 和 OR 通過使用 AND 和 OR,可以在我們的 XPath 表達式中放入 2 個條件。 * 如果 AND 為 2,則兩個條件均應為 true,則只有找到條件的元素。 * 如果為 OR,則兩個條件中的任何一個都應為 true,然后只有它才能找到該元素。 在這里,我們的 XPath 查詢將像 ``` Xpath=//*[@type='submit' OR @name='btnReset'] ``` ``` Xpath=//input[@type='submit' and @name='btnLogin'] ``` ![](https://img.kancloud.cn/f2/f9/f2f9a78f16b6d970d19dce6d0e63b5f1_581x287.png) 測試步驟: 1. 轉到 [http://demo.guru99.com/v1/](http://demo.guru99.com/v1/) 2. 在本節中,將使用上面的演示站點來搜索具有 XPath 不同功能的元素。 您將找到使用 AND 和 OR,父代,始于和 XPath 軸的元素 ### AND OR 范例 ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class AND_OR { public static void main(String[] args) { WebDriver driver; WebElement w,x; System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe"); driver= new ChromeDriver(); // Launch the application driver.get("https://www.guru99.com/"); //Search element using OR in the xpath w=driver.findElement(By.xpath("//*[@type='submit' OR @name='btnReset']")); //Print the text of the element System.out.println(w.getText()); //Search element using AND in the xpath x=driver.findElement(By.xpath("//input[@type='submit' and @name='btnLogin']")); //Print the text of the searched element System.out.println(x.getText()); //Close the browser driver.quit(); } } ``` ## 父級 通過使用父級,您可以在網頁中找到當前節點的父級節點。 Here our XPath query will be like ``` Xpath=//*[@id='rt-feature']//parent::div ``` ![](https://img.kancloud.cn/26/75/2675f27368a27c2df622a25286875896_683x473.png) ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Parent { public static void main(String[] args) { WebDriver driver; WebElement w; System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe"); driver= new ChromeDriver(); // Launch the application driver.get("https://www.guru99.com/"); //Search the element by using PARENT w=driver.findElement(By.xpath("//*[@id='rt-feature']//parent::div")); //Print the text of the searched element System.out.println(w.getText()); //Close the browser driver.quit(); } } ``` ## 開頭為 使用 Starts-with 函數,您可以找到其屬性在刷新或其他操作(例如單擊,提交等)時動態變化的元素。 Here our XPath query will be like ``` Xpath=//label[starts-with(@id,'message')] ``` [![](https://img.kancloud.cn/0c/4d/0c4d0383ba36e0f7ab95340abb49878d_942x412.png) ](/images/1/081319_1305_XPathContai9.png) ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class StartsWith { public static void main(String[] args) { WebDriver driver; WebElement w; System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe"); driver= new ChromeDriver(); // Launch the application driver.get("https://www.guru99.com/"); //Search the element by using starts-with w=driver.findElement(By.xpath("//label[starts-with(@id,'message')]")); //Print the text of the searched element System.out.println(w.getText()); //Close the browser driver.quit(); } } ``` ## Xpath 軸 通過使用 XPath 軸,您可以在網頁上找到動態且非常復雜的元素。 XPath 軸包含幾種查找元素的方法。 在這里,將討論幾種方法。 之后的**:此函數將返回特定組件的直接元素。** Here our XPath query will be like ``` Xpath=//*[@type='text']//following::input ``` ![](https://img.kancloud.cn/ff/15/ff1578f943cacca2cffde70eb913986d_768x360.png) ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Following { public static void main(String[] args) { WebDriver driver; WebElement w; System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe"); driver= new ChromeDriver(); // Launch the application driver.get("https://www.guru99.com/"); //Search the element by using Following method w=driver.findElement(By.xpath("//*[@type='text']//following::input")); //Print the text of the searched element System.out.println(w.getText()); //Close the browser driver.quit(); } } ``` **前面的內容:**此函數將返回特定元素的前面的元素。 Here our XPath query will be like ``` Xpath= //*[@type='submit']//preceding::input ``` ![](https://img.kancloud.cn/b8/f4/b8f4bc506c74630be6f735588dfa14b2_923x283.png) ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Preceding { public static void main(String[] args) { WebDriver driver; WebElement w; System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe"); driver= new ChromeDriver(); // Launch the application driver.get("https://www.guru99.com/"); //Search the element by using preceding method w=driver.findElement(By.xpath("//*[@type='submit']//preceding::input")); //Print the searched element System.out.println(w.getText()); //Close the browser driver.quit(); } } ``` d)**后代:**此函數將返回特定元素的后代元素。 Here our XPath query will be like ``` Xpath= //*[@id='rt-feature']//descendant::a ``` ![](https://img.kancloud.cn/95/1c/951c24cdf4497ed626056d17bcec8d32_616x429.png) ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Descendant { public static void main(String[] args) { WebDriver driver; WebElement w; System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe"); driver= new ChromeDriver(); // Launch the application driver.get("https://www.guru99.com/"); //Search the element by using descendant method w=driver.findElement(By.xpath("//*[@id='rt-feature']//descendant::a")); //Print the searched element System.out.println(w.getText()); //Close the browser driver.quit(); } } ``` ### 摘要 * 在某些情況下,無法使用常規 XPath 查找元素。 在這種情況下,我們需要與 xpath 查詢不同的功能。 * 這里有一些重要的 XPath 函數,例如 contains,parent,祖先,following-sibling 等。 * 借助這些功能,您可以創建復雜的 XPath 表達式。
                  <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>

                              哎呀哎呀视频在线观看