java - How do I append a "descendant" clause next to an xpath variable? -


this code looks like

    public void sometest() {       string x = "/html/body/div/div["       string y = "]/a"          (int = 1; < 5; i++){             string links = x + + y;             driver.findelement(by.xpath(links)).click(); // iteratively go next link , click on          }     } 

what i'm trying achieve, however, once clicks on link, should descendant links , click on well. there way of doing that? if try following, work?

    driver.findelement(by.xpath(links/descendant::a).click();  

it's fragile way of coding, altering xpath go through loop. instead, recommend using findelements method , looping through elements returns, this:

public void sometest() {  xpath xpath_findlinks = "/html/body/div/div/a";  xpath relative_xpath = "./..//a";  (webelement eachlink : driver.findelements(by.xpath(xpath_findlinks))) {   eachlink.click();   (webelement descendantlink : eachlink.findelements(by.xpath(relative_xpath))) {    descendantlink.click();   }  } } 

note crucial difference. first time call findelements it's on driver, it's looking through entire html; second time, it's called method of a particular element (the current link), can use find elements relative link - e.g. find descendants.

i recommend using structure; however, without knowing overall html it's hard know relative xpath use. 1 have provided example of relative xpath might like, distinctive start of ./.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -