How to trim the string results | Selenium Forum
M
Posted on 10/02/2016
Hi
I want to print links text which are on a panel. But It printing extra space when there is nothing.

How could I take off that extra space.

Screenshots attached.

Thanks in advance.

Here is my code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.List;

/**
* Created by samchitreddy on 10/02/2016.
*/
public class RBSTEST {
public static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.get("http://personal.rbs.co.uk/personal.html");
driver.manage().window().maximize();
WebElement personal = driver.findElement(By.id("list-a"));

List<WebElement> personalLinks = driver.findElements(By.id("list-a"));
for (int i=0; i<personalLinks.size(); i++){
System.out.println(personalLinks.get(i).getText().trim());
driver.findElement(personalLinks(i)).click();
}
System.out.println(personalLinks.size());
driver.quit();
}
}

M
Replied on 10/02/2016

Before printing the link text check the visibility of the link. Use isDisplayed() method.


M
Replied on 10/02/2016

There are total 4 links but its printing only one when take size of the Web elements. Is there anything wrong in it?

I will try using displayed and I will get you.

Thanks
Sam


M
Replied on 10/02/2016

Hi Mohith,

I tried using isDisplayed. But it still giving me the same result. And size also wrong as it should be 4.

I am doing mistake somewhere, but could not find it. Any more suggestions.

thanks in advance
Sam


M
Replied on 10/02/2016

that was my mistake.

Got it now.

Here is the new code

List<WebElement> personalLinks = driver.findElements(By.xpath(".//*[@class=wg-c-nav-internal__list']/a"));

I did not give right xpath as I gave to find only one element.

Thanks
Sam