java - Webdriver findElements By xpath -


1)i doing tutorial show how findelements xpath works. know why returns texts following <div> element attribute id=container.

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

2) how should modify code return first or first few nodes follow parent note e.g. first node 'home', first few node like, home, manual testing , automation testing.

thanks advise , help!

here code fragment 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());             }          }         {             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 attribute value=container                system.out.println("the id value is: " + webelement.getattribute("id"));                system.out.println("the tag name is: " + webelement.gettagname());            }            {                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 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("");           }    }    {        driver.close();    }  }//end of xpathdemo3  } 

your questions:

q 1.) know why returns texts following div?
should not , think in not. returns div 'id' attribute value equal 'containter' (and children of this). printing results ele.gettext() gettext return text content of children of result.

get visible (i.e. not hidden css) innertext of element, including sub-elements, without leading or trailing whitespace.
returns:
innertext of element.

q 2.) how should modify code return first or first few nodes follow parent note
not clear looking for. example:

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

the following parent of div p2. be:

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

or shorter

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

if looking first 1 extent expression "predicate" (e.g [1] - first one. or [position() &lt; 4]for first three)

if looking first child of first div:

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

if there 1 div id looking first child:

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

and on.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -