Home > Back-end >  I have two Ids in the same Row where the Id is seperated by Comma and wanted only one Id how to get
I have two Ids in the same Row where the Id is seperated by Comma and wanted only one Id how to get

Time:09-17

Scenario:

As seen in the above screen there are 2 ids in the same row which are seperated by comma.

My method is there where i get the lastest id.The Problem over here is it fetches both the Ids. Since i use the id to search i dont want both the Ids.

Below is my method which i use to Fetch the Ids

public static HashMap<String, String> Values; 
    
public static void PlanningScreenFetchPlanningandDemandID(WebDriver driver) throws InterruptedException
    {
                    
        Values = new HashMap<String, String>();

        
        
        List<WebElement> tableValue = driver.findElements(By.xpath("//table//tr//td[contains(@class,'mat-column-demandId')]//span"));

        int tableValueSizes = tableValue.size();

        WebElement latestIds = driver.findElement(By.xpath("(//table//tr//td[contains(@class,'mat-column-demandId')]//span)[" tableValueSize  "]"));

        
        
        Values.put("latestDataId", latestIds.getText()); 
        
        
        System.out.println(Values.put("latestDataId", latestIds.getText()));
        
        
        String Id = AppXPathsConstants.columnInputEntryXpath_replace.replace("XXXX", "ID") ;
        inputEntry(driver, Id , Values.put("latestDataId", latestId.getText()));
        
        
    }

CodePudding user response:

If latestIds.getText() prints this 4306, 4307 in the console, and your intention is to get only id (I'd assume the first one), then you can try the below code :

String firstID = latestIds.getText().split(",")[0];
String secondID = latestIds.getText().split(",")[1];

also if there are trailing spaces, make sure to use .trim to remove them.

CodePudding user response:

As I understand you are getting Ids here:

latestIds.getText()

So you can simply split the Id values as following:

String bothIds = latestIds.getText();  
String [] ids = bothIds.split(",");

To give better answer we need to get more details about that web page, not a pictures
UPD
You can get each separate id from the array above simply as following: First id value:

ids[0]

The second id value:

ids[1]
  • Related