場景:當定位某個元素時,發現所需要的元素在同級節點,可以用/following-sibling::* 方法(定位同級的第二位)
當定位統計節點的第二個定位相鄰節點。 可以用/preceding-sibling::* (定位同級的第一位)
selenium定位父子、兄弟、相鄰節點定位方法。
案例(一):需要定位到關閉按鈕。
Xpath寫法:
.//span[@id='ui-id-1']//following-sibling::*
二、定位Table 的Xpath 定位。‘
Xpath 寫法: //table[contains(@class,'hover table-Center')]/tbody/tr
public static String getIndex(String tableXpath, String column, WebDriver driver) { List<WebElement> headList = driver.findElements(By.xpath(tableXpath + "/thead//td")); for (int i=0;i<headList.size();i++) { if (headList.get(i).getAttribute("innerText").trim().equals(column)) { return String.valueOf(i+1); } } throw new RuntimeException("找不到列名: " + column); }