0

On the Virgintrains.co.uk website trying to select a arrival train station

In my Code I am able to select the departure location and in the next field I would like to select a arrival location and then tab to the date.

The issue i am having it send the name of the arrival location but it does not commit it and move to the next field.

I have tried to use a tab key so it goes to next field, i have tried select by visible test,

`import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class VirginTrains {

    @SuppressWarnings("null")
    public static void main(String[] args) throws InterruptedException {

        String projectPath = System.getProperty("user.dir");
        System.out.println("projectPath : " + projectPath);

        //Opens virgintrains webpage
        System.setProperty("webdriver.chrome.driver", projectPath + "\\drivers\\chromedriver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.virgintrains.co.uk/");

        //The following maximise the screen size
        driver.manage().window().maximize();

        //The following selects the Departure train station
        WebElement textBoxDep = driver.findElement(By.name("origin_station"));
        textBoxDep.sendKeys("London Euston");

        //ADD WAIT STATMENT
                //Thread.sleep(10000);

THIS SECTION BELOW IS WHERE I AM HAVING THE ISSUE

        // Following selects the Arrival train station
        WebElement textBoxArr = driver.findElement(By.name("destination_station")); 
        textBoxArr.sendKeys("   Manchester Piccadilly");
       // WebElement option = null;
        //option.click();

      //ADD WAIT STATMENT
            Thread.sleep(10000);

        WebElement webElement = null;
        webElement.sendKeys(Keys.TAB);

        WebElement webElement2 = null;
        webElement2.sendKeys(Keys.TAB);`

I am expecting the code to select the required train station and then move to the date field.

The actual result is that it is populating the field but not selecting the station and moving to the next field.

  • @sarabit this is not good practice to use Thread.sleep() every time. you can refer my answer how to find auto populate elements and select it using list. it will far better than using key and Thread every time. Please refer this link - for my answer. https://stackoverflow.com/questions/55467134/how-handle-auto-suggest-in-from-and-destination-box-for-this-website-https – Dhru 'soni Apr 11 '19 at 05:53

1 Answers1

1

You are trying to send a Tab press to null elements. Instead of separating the location and the tab key press, put them on the same line:

textBoxDep.sendKeys("London Euston" + Keys.TAB);

textBoxArr.sendKeys("Manchester Piccadilly" + Keys.TAB + Keys.TAB);

Also, Thread.sleep is not suggested. You can use a wait for a specific condition before proceeding instead. More info on waits here: https://www.toolsqa.com/selenium-webdriver/wait-commands/

RKelley
  • 1,099
  • 8
  • 14
  • Thank you very much for your reply I tried you idea that field seems to be a suggestion box - i enter Manchester Piccadilly and then it will be in a suggestion field and i need to select that value. // Following selects the Arrival train station WebElement textBoxArr = driver.findElement(By.name("destination_station")); // textBoxArr.sendKeys(" Manchester Piccadilly"); textBoxArr.sendKeys(" Manchester Piccadilly" + Keys.TAB); – Sarabjit Bhamber Apr 10 '19 at 21:33
  • I noticed that doing this manually does not work as expected in your code. I did not enter the leading spaces and had to hit tab twice for it to work. Check my update in the answer. – RKelley Apr 10 '19 at 21:45
  • Thank you very much for all your help - It has now working and taking the value in – Sarabjit Bhamber Apr 11 '19 at 07:26
  • If my answer helped fix your issue please accept it as the answer. https://stackoverflow.com/help/someone-answers Thanks! – RKelley Apr 11 '19 at 17:04