Let us take a scenario in which you need to verify number of records displayed in the page.
So from the web page text - 2 products found. We want to extract only 2. This can be achieved in below steps.
1) Write xpath to get text from web page,
String result_Text = driver.findElement(By.xpath("/html/body/table[5]/tbody/tr/td")).getText();
Note: In this case, used getText() because text is in html and not in attribute. If it is in attribute, you could use getAttribute();
2) Use indexOf method in java, that would return the position of particular string or character something like this:
int indexOfColon = result_Text.indexOf(':');
Similarly get index of products
int indexOfProducts = result_Text.indexOf('products');
3) Now use, subString() method in Java to get number what you are looking for:
String textReturned = result_Text.substring(indexOfColon+1, indexOfProducts).trim();
Now, textReturned is containing only number of products you are looking for.
You can use, Integer.parseInt(textReturned) to convert String value to int datatype.
Hope this blog has been useful to you.
1) Write xpath to get text from web page,
String result_Text = driver.findElement(By.xpath("/html/body/table[5]/tbody/tr/td")).getText();
Note: In this case, used getText() because text is in html and not in attribute. If it is in attribute, you could use getAttribute();
2) Use indexOf method in java, that would return the position of particular string or character something like this:
int indexOfColon = result_Text.indexOf(':');
Similarly get index of products
int indexOfProducts = result_Text.indexOf('products');
3) Now use, subString() method in Java to get number what you are looking for:
String textReturned = result_Text.substring(indexOfColon+1, indexOfProducts).trim();
Now, textReturned is containing only number of products you are looking for.
You can use, Integer.parseInt(textReturned) to convert String value to int datatype.
Hope this blog has been useful to you.
No comments:
Post a Comment