让所有直接的孩子,没有更深层次

时间:2011-01-06 01:07:32

标签: xpath webdriver

WebElement body = browser.findElement(By.xpath("//body"));

body.findElement(By.xpath("")); // I want to get all child elements 
                                // inside body, but nothing deeper.

示例文档。

<html>
  <body>
    <div>
    </div>
    <span>
      <table>
      </table>
    </span>
  </body>
</html>

预期结果为divspan。我无法控制文件,而且差异很大。

4 个答案:

答案 0 :(得分:30)

("*")给出上下文节点的所有子元素。所以使用:

body.findElement(By.xpath("*"));

答案 1 :(得分:6)

/html/body/*

将仅选择body的直接子元素。

请记住,如果复制所有这些选定节点,则还要复制其内容。因此,如果您执行copy-of,则还会在生成的文档中生成table

另外,我建议至少阅读XPath基础知识,你会问太多类似的问题。

答案 2 :(得分:4)

这是获取元素的直接子元素的另一种方法:

element.findElement(By.xpath("./*"));

答案 3 :(得分:1)

child代替public class Images { private String[]eyes=new String[]{ "images/character/face/eyes/blue.png" }; public LinkedList<Bitmap> eyesBM=new LinkedList<Bitmap>(); private int eyesIndex = 0; private String[]faceandhands=new String[]{ "images/character/face/faceandhands/white.png", "images/character/face/faceandhands/black.png" }; public LinkedList<Bitmap> faceandhandsBM=new LinkedList<Bitmap>(); private int faceandhandsIndex = 0; private String[]facialhair=new String[]{ "images/character/face/facialhair/black.png" }; public LinkedList<Bitmap> facialhairBM=new LinkedList<Bitmap>(); private String[]hair=new String[]{ "images/character/face/hair/grey.png" }; public LinkedList<Bitmap> hairBM=new LinkedList<Bitmap>(); private String[]tattoes=new String[]{ "images/character/face/tattoes/test.png" }; public LinkedList<Bitmap> tattoesBM=new LinkedList<Bitmap>(); private String[]legs=new String[]{ "images/character/legs/brown.png" }; private LinkedList<Bitmap> legsBM=new LinkedList<Bitmap>(); private String[]shirt=new String[]{ "images/character/shirt/purple.png" }; public LinkedList<Bitmap> shirtBM=new LinkedList<Bitmap>(); private String[]shoes=new String[]{ "images/character/shoes/red.png" }; public LinkedList<Bitmap> shoesBM=new LinkedList<Bitmap>(); private String[]background=new String[]{ "images/character/background/normal.png" }; public LinkedList<Bitmap> backgroundBM=new LinkedList<Bitmap>(); public Images(Context c) { loadBitmaps(c,eyes,"eyes"); loadBitmaps(c,faceandhands,"faceandhands"); loadBitmaps(c,facialhair,"facialhair"); loadBitmaps(c,hair,"hair"); loadBitmaps(c,tattoes,"tattoes"); loadBitmaps(c,legs,"legs"); loadBitmaps(c,shirt,"shirt"); loadBitmaps(c,shoes,"shoes"); loadBitmaps(c,background,"background"); } public Bitmap getBitmapFromAsset(Context context, String filePath) { AssetManager assetManager = context.getAssets(); InputStream istr; Bitmap bitmap = null; try { istr = assetManager.open(filePath); bitmap = BitmapFactory.decodeStream(istr); } catch (IOException e) { System.out.println("Fudeu geral !"); } return bitmap; } public void loadBitmaps(Context c,String[] src,String dest){ Bitmap bit=null; for (int i = 0;i<src.length;i++){ bit= getBitmapFromAsset(c,src[i]); switch (dest){ case "eyes": eyesBM.add(bit); break; case "faceandhands": faceandhandsBM.add(bit); break; case "facialhair": facialhairBM.add(bit); break; case "hair": hairBM.add(bit); break; case "tattoes": tattoesBM.add(bit); break; case "legs": legsBM.add(bit); break; case "shirt": shirtBM.add(bit); break; case "shoes": shoesBM.add(bit); break; case "background": backgroundBM.add(bit); break; } //dest.add(bit); //dest.add(BitmapFactory.decodeResource(r, src[i])); } } public Bitmap getBitmap(String name){ switch (name){ case "eyes": eyesIndex=eyesIndex>=eyesBM.size()?0:eyesIndex; Bitmap aux =eyesBM.get(eyesIndex); eyesIndex++; return aux; case "faceandhands": faceandhandsIndex=faceandhandsIndex>=faceandhandsBM.size()?0:faceandhandsIndex; Bitmap aux1=faceandhandsBM.get(faceandhandsIndex); faceandhandsIndex++; return aux1; //return faceandhandsBM.get(0); case "facialhair": return facialhairBM.get(0); case "hair": return hairBM.get(0); case "tattoes": return tattoesBM.get(0); case "legs": return legsBM.get(0); case "shirt": return shirtBM.get(0); case "shoes": return shoesBM.get(0); case "background": return backgroundBM.get(0); } return null; }} 可以帮助某人。

相关问题