Trying to automate the IRCTC site but facing NullPointerException while filtering the train from the search results

17 Views Asked by At

here is the snippet of the code which is causing problem.

        //list of trains after clicking on the search button
        List<WebElement> listOfTrains = driver.findElements(By.xpath("//div[@class='form-group no-pad col-xs-12 bull-back border-all']"));

        //filtering the specific train you want to book
        WebElement desiredTrain=listOfTrains.stream()
                                            .filter(train->train.findElement(By.xpath("//div[@class='col-sm-5 col-xs-11 train-heading']/strong"))
                                            .getText()
                                            .contains("AADR VANDEBHARAT (22448)"))
                                            .findFirst()
                                            .orElse(null);

In above case, 'AADR VANDEBHARAT (22448)' is the first WebElement in the list of WebElements (listOfTrains). listOfTrains has already 38 WebElements in it, which are the different trains appeared after clicking on search button.

The main problem is that, when choosing first element of the list (viz., the div containing 'AADR VANDEBHARAT (22448)'), it's working fine, and I'm able to automate till payment page. But the thing turns south, when I choose train other than the first one (like 'SVDK VANDEBHARAT (22440)'). It'll give me the NullPointerException. I've attached some images for your reference.

NullPointerException

IRCTC webpage

0

There are 0 best solutions below