Refer to the below Links for Practise |
Complete JAVA QUIZ Selenium Advanced Quiz 50 Real Time Interview Questions Selenium Quiz REST API QUIZ |
Dynamic Content:
In Web Applications the data in the HTML Table is dynamic always. To Fetch the data available in the Dynamic Table or to identify the Dynamic Web Elements available in the Web Page we should depend on Xpath Axes Locators. |
Following are the Xpath Axes Locators:
Link for XPATH AXES->
https://www.w3schools.com/xml/xpath_axes.asp
• ancestor – select parent or grand parent
• following-sibling – Which follows
• preceding-sibling – Which precedes
• descendant – Child/subChild
• parent – Select Immediate Parent
• child- Select immediate Child
• following – Select all the matching nodes which are following
• preceding – Select all the matching nodes which are preceding
Real Time Example:
1. Access the Web Application URL -> https://money.rediff.com/gainers/bse/daily/groupa?src=gain_lose 2. Find the Stock Name ->‘Adani Enterprises Lt’. 3. Get the Price associated with the Stock Name. |
Identify the following Xpath in Firepath for understanding:
Xpath Example 1:
Identify the Parent of the WebElement
//a[contains(text(),'Adani Enterprises')]/parent::td
Xpath Example 2:
Identify the Ancestor of the Web Element
//a[contains(text(),'Adani Enterprises')]/parent::td
Xpath Example 3:
Identify the following Elements of the Web Element
In the Case of following all the Elements which are matching will be selected. Now the matching nodes are 501.
//a[contains(text(),'Adani Enterprises')]/parent::td/following::td
Xpath Example 4:
Identify the following siblings of the Web Element
In the Case of following-sibling all the Elements which are matching but should be part of the parent tag ‘tr’. Now the matching nodes are 4.
//a[contains(text(),'Adani Enterprises')]/parent::td/following-sibling::td
Fetching the Price associated with the Stock:
//a[contains(text(),'Adani Enterprises')]/parent::td/following-sibling::td[2]
Selenium Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package org.totalqa.selenium; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class XpathAxesLocators { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "geckodriver.exe"); FirefoxDriver driver = new FirefoxDriver(); driver.get("https://money.rediff.com/gainers/bse/daily/groupa?src=gain_lose"); /** * Solution 1 using Axes Locators */ WebElement e = driver.findElement(By.xpath("//a[contains(text(),'Adani Enterprises')]/parent::td/following-sibling::td[2]")); try{ if(e.isDisplayed()) { String price = e.getText(); System.out.println("Stock Price" + price); } } catch(NoSuchElementException ex) { System.out.println("Stock Name Not Exists"+ ex.getMessage()); } /** * Solution 2 using findElements() */ List<WebElement> trList = driver.findElements(By.xpath(".//*[@id='leftcontainer']/table/tbody/tr/td[1]/a")); for(int i=0;i<trList.size();i++) { if(trList.get(i).getText().contains("Adani-Enterprises")) { e = driver.findElement(By.xpath(".//*[@id='leftcontainer']/table/tbody/tr["+(i+1)+"/td[3]")); System.out.println("Stock Price:::" + e.getText()); break; } } } } |