Quality Testing

Quality is delighting customers

For Ex:

For the first time when I inspect an element it's Xpath is "123", and the script worked and the next day, the script fails because the xpath differs due to dynamic element. Even there is no attributes like Id,name etc... to that web element.

So how to handle this situation ?

Views: 295

Reply to This

Replies to This Discussion


Handle dynamic elements --the scenario is for ex: in signup pages we have fields like 1+6=?, in these 1 and 6 are changes for every fresh page(refresh the page), how can we answer these below the code for these 

String text1 = driver.findElement(By.xpath("//input[@id='num1']")).getAttribute("value"); //this is for getting the value as 1 in our example
System.out.println(text1 + "is displayed");
String text2 = driver.findElement(By.xpath("//input[@id='num2']")).getAttribute("value");//this is for getting the value as 6 in our example

int n1 = Integer.parseInt(text1);
int n2 = Integer.parseInt(text2);

int Sum = n1 + n2;
String S = String.valueOf(Sum);
System.out.println("sum is" + S);
driver.findElement(By.xpath(--give the element path of ?---)).click();
driver.findElement(By.xpath(--give the element path of ?---)).clear();
driver.findElement(By.xpath(--give the element path of ?---)).sendKeys(S);//here enter value as 7(1+6) our example

-->Like this we can handle the dynamic elements and other way is to handle dynamic xpath is  as mentioned below

we have an xpath like-->Xpath = .//*[@id='select2-result-label-535']

for (int i=0:;i<=9:i++){

String mpath = ".//*[@id='select2-result-label-535'] "+ i+" ']";



correct me if am wrong means, i think this will helpful to u :-)


Hi Sudharshan

Thanx fr ur response. Well the point is my question doesn't reached you well it seems.

Scenario is :

Just for an Ex:

1.Login to HDFC bank

2.Inspect the Transfer element to find it's Xpath, hence there is no other attributes like ID,name, etc...

3.Refresh the page and again inspect that same element, the Xpath differs from previous inspection.

4.So my quest is how to run the script ?


TTWT Magazine





© 2019   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service