How to handle dynamic objects in webdriver and How to Get all links in a page and count of the links in webdriver

by admin on June 30, 2014

if your dynamic element’s ids have the format where button id=”continue-12345″ where 12345 is a dynamic number you could use the following

XPath: //button[starts-with(@id, ‘continue-‘)]

int i=0;
List<WebElement> allElements = driver.findElements(By.xpath(“//*[@id=’admin’]/ul/li”));
for (WebElement Element : allElements) {
i = i+1;
// System.out.println(Element.getTagName());
System.out.println(“total objects founds ” + i);


My Xpath is changing ilke starting some value is ame but ending is differnt  You need to use regular expression like

//input[@id *= ‘12345’]

The asterix will take care if the id name changes either in beginning or in the end, but remember some part of the string should not change

but i have ten fields in my web page all similar kind i dont know which one will be taken if i use starts with how to idenitfy these cases?

To get All links try this

//Get all links of page
List<WebElement> allLinks = driver.findElements(By.tagName(“a”));
for(int i =0; i<allLinks.size(); i++) {

//make sure you get the text of only visible links and not hidden links


//Initialize Webdriver
FirefoxDriver fd=new FirefoxDriver();
//go to url
fd.get(type url…..”);
//get all links web element
List<WebElement> alllinks=fd.findElements(By.tagName(“a”));
//print all elements
System.out.println(“link size: “+alllinks.size());
//Loop to get all links
for(WebElement link : alllinks)

For dynamically changing objects : use RE

Regular Expression: it is used to recognize the test/element which dynamically changing
RE specify the pattern, if pattern matches then it will recognize the text or element.

#SQASolutionShare on FacebookShare on Google+Share on LinkedInTweet about this on TwitterEmail this to someone

Previous post:

Next post: