Webdriver findElements通过xpath

Webdriver findElements通过xpath

问题描述:

1)我正在做一个教程来展示xpath的findElements如何工作.我想知道为什么它返回带有属性id=container<div>元素之后的所有文本.

1)I am doing a tutorial to show how findElements By xpath works. I would like to know why it returns all the texts that following the <div> element with attribute id=container.

xpath的代码:By.xpath("//div[@id='container']

code for xpath: By.xpath("//div[@id='container']

2)我应该如何修改代码,使其仅返回父注释后面的第一个或前几个节点 例如第一个节点(如家庭"),前几个节点(如家庭,手动测试和自动化测试).

2) how should I modify the code so it just return first or first few nodes that follow the parent note e.g. first node like 'Home', first few node like, Home, Manual Testing and Automation Testing.

感谢您的建议和帮助!

这是本教程的代码片段:

Here is the code fragment for this tutorial:

import java.util.List;

import org.junit.Test;
import org.junit.Before;
import org.junit.After;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WD_findElements 
{
    @Test
    public void test_byxpath(){

        WebDriver driver = new FirefoxDriver();

        try{
            driver.get("http://www.hexbytes.com");

        List<WebElement> elements = driver.findElements(By.xpath("//div[@id='container']"));    
            System.out.println("Test7 number of elements: " + elements.size());

            for(WebElement ele : elements){
                //ele.sendKeys("hexbyes");
                System.out.println(ele.getText());
                //System.out.println(ele.getAttribute("id"));
                //System.out.println(ele.getTagName());
            } 
        }
        finally {
            driver.close();
        }

    }//end of test_byxpath

 public void xpathDemo2() {
           WebDriver driver = new FirefoxDriver();
           try{
               driver.get("http://www.hexbytes.com");
               WebElement webelement = driver.findElement(By.id("container"));
               //matching single element with attribute value=container
               System.out.println("The id value is: " + webelement.getAttribute("id"));
               System.out.println("The tag name is: " + webelement.getTagName());
           }
           finally {
               driver.close();
           }
       }//end of xpathDemo2 

public void xpathDemo3() {
       WebDriver driver = new FirefoxDriver();
       try{
           driver.get("http://www.hexbytes.com");
          //find first child node of div element with attribute=container
          List<WebElement> elements = driver.findElements(By.xpath("//div[@id='container']/*[1]"));
          System.out.println("Test1 number of elements: " + elements.size()); 

          for(WebElement ele : elements){
              System.out.println(ele.getTagName());
              System.out.println(ele.getAttribute("id"));
              System.out.println("");
              System.out.println("");
          }
   }
   finally {
       driver.close();
   }
 }//end of xpathDemo3
 }

您的问题:

问题1.)我想知道为什么它返回div之后的所有文本吗?
它不应该,我认为不会.它返回所有具有"id"属性值等于"containter"的div(及其所有子级).但是您正在使用ele.getText()打印结果 其中,getText将返回结果的所有子级的所有文本内容.

Q 1.) I would like to know why it returns all the texts that following the div?
It should not and I think in will not. It returns all div with 'id' attribute value equal 'containter' (and all children of this). But you are printing the results with ele.getText() Where getText will return all text content of all children of your result.

获取此元素的可见(即未被CSS隐藏)innerText,包括子元素,而没有任何前导或尾随空格.
返回:
此元素的innerText.

Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.
Returns:
The innerText of this element.

问题2.)如何修改代码,使其仅返回父注释后面的第一个或前几个节点
这不是很清楚您在寻找什么. 示例:

Q 2.) how should I modify the code so it just return first or first few nodes that follow the parent note
This is not really clear what you are looking for. Example:

<p1> <div/> </p1 <p2/> 

div的父级以下是p2. 这将是:

The following to parent of the div is p2. This would be:

 //div[@id='container'][1]/parent::*/following-sibling::* 

或更短

 //div[@id='container'][1]/../following-sibling::* 

如果您只在寻找第一个范围,则带有谓词"的表达式 (例如[1]-第一个.或[position() &lt; 4]前三个)

If you are only looking for the first one extent the expression with an "predicate" (e.g [1] - for the first one. or [position() &lt; 4]for the first three)

如果您正在寻找第一个div的第一个孩子:

If your are looking for the first child of the first div:

//div[@id='container'][1]/*[1]

如果只有一个id为的div,则您正在寻找第一个孩子:

If there is only one div with id an you are looking for the first child:

   //div[@id='container']/*[1]

以此类推.