Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
1.3k views
by The go-to Tester (218 points)

I am writing automated tests for node.js applicatioon. The application has parent and child nodes. I want to access parent and child nodes. Which locator should I use. The application is at-:

http://bl.ocks.org/robschmuecker/7926762

1 Answer

0 like 0 dislike
by
selected by
 
Best answer

There are two ways you can get the parent node.

Using JavaScript and using xPath.

1.) Using Javascript, you will have to use parentNode function to get the parent of an element.

Eg.

WebElement ele = driver.findElement(By.id("eleID"));
WebElement theParent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", ele );
 
2.) Using xPath, you can use ".." double dot to get the parent of the element.
Eg.
WebElement ele = driver.findElement(By.id("eleID"));
WebElement theParent = ele.findElement(By.xpath(".."));
 
To get the child element, you can simply identify element using xPath, cssSelector or tagName
Eg. Using tagName
 
WebElement eleTable = driver.findElement(By.id("TableID"));
WebElement theFirstRow = eleTable.findElements(By.tagName("tr")).get(0);
 
 


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...