Home > Software design >  Trying to click a Button called “Book Now” with Selenium and Java but get “could not be scrolled int
Trying to click a Button called “Book Now” with Selenium and Java but get “could not be scrolled int

Time:11-01

I am attempting to create a test script that books an event… but I am trying to select and click the “Book Now” button on this page.. https://www.trybooking.com/events/landing?eid=757231&

clicking on the "Book Now" button should take the booker to the sessions page.. https://www.trybooking.com/book/sessions?eid=757231

Instead my code causes a Junit/Java exception...

I have made may attempts to select the button and click on it so that it goes to the sessions page.. with the following... [these have been commented out in the code]...

    String url = "https://www.trybooking.com/events/landing?eid=757231&";
    myDriver.get(url);
    //WebElement we = myDriver.findElement(By.xpath("//span/button[text()='Book now' and @class='btn btn-secondary text-uppercase btn-landing go-to-session']"));
    //WebElement we = myDriver.findElement(By.xpath("//span/button[text()='Book now']"));
   // WebElement we = myDriver.findElement(By.xpath("//span/button[text()='BOOK NOW']"));
    //WebElement wb = myDriver.findElement(By.xpath ("//*[contains(text(),'Book now')]"));
    //WebElement wb = new WebDriverWait(myDriver, Duration.ofSeconds(50, 1)).
    //WebElement wb = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'Book now')]")));
    //WebElement wb = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'Book now') ]")));
    //WebElement wb = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-secondary text-uppercase btn-landing go-to-session']" )));
    //wb.click();
    //wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-secondary text-uppercase btn-landing go-to-session']" ))).click();
    
    //WebElement wb = myDriver.findElement(By.xpath ("//button[@class='btn btn-secondary text-uppercase btn-landing go-to-session']"));
    //WebElement wb = myDriver.findElement(By.xpath ("//div[@class='pl-0 mr-3 sticky-btn-wrapper'] and button[@class='btn btn-secondary text-uppercase btn-landing go-to-session']"));
    //WebElement BookNowButton = myDriver.findElement(By.xpath ("//div[@class='pl-0 mr-3 sticky-btn-wrapper']//button"));

My latest attempt and selenium code...My selenium code to select this is ...

    // added at the start of the Junit ...  JavascriptExecutor js = (JavascriptExecutor) myDriver;
@DisplayName("TC03: Book into an event (ADVANCED TASK)")
         void Test3() 
     // commented out.. throws Exception
     {
        //myDriver.get("https://www.trybooking.com/book/search");

        
        WebDriverWait wait = new WebDriverWait(myDriver, Duration.ofSeconds(10, 1));
        myDriver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        
        String url = "https://www.trybooking.com/book/search";
        myDriver.get(url);

        myDriver.findElement(By.id("initSearchKeywordId")).click();
        myDriver.findElement(By.id("initSearchKeywordId")).sendKeys("Makerspace Docklands - Safety Induction");
        myDriver.findElement(By.id("initSearchButtonId")).click();
     
        wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("pre-load-spinner") ));
        WebElement we = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='search-heading' and starts-with(@id, 'newSearchNavigateHeadingEventId')]/h2[contains(., 'Makerspace Docklands - Safety Induction') and contains(@ng-bind, 'EventDisplayName')]")));
        we.click();
        
       
        // now to click on the book Now button on the "Makerspace Docklands - Safety Induction" page 
        
        WebElement BookNowButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath( "//div[@class='pl-0 mr-3 sticky-btn-wrapper']//button"  )));
        //This will scroll the page till the element is found       
        js.executeScript("arguments[0].scrollIntoView();",BookNowButton);
        BookNowButton.click();
    // more code to follow
    }// close void Test3()

I get the following error message…

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.JavascriptExecutor.executeScript(String, Object[])" because "this.js" is null

Any suggestions?

CodePudding user response:

Null point exception happens when driver instance is not interact by selenium in every class

Solution - define webdriver driver in global variable

WebDriver driver;

CodePudding user response:

Before these statements

String url = "https://www.trybooking.com/events/landing?eid=757231&";
myDriver.get(url);
  1. Define the myDriver object, and Create respective Browser reference too
WebDriver myDriver; 

//Define Globally above all Methods

  1. Define Driver exe path or use WebDriverManager of bonigarcia ex: for chrome
System.setProperty("webdriver.chrome.driver", driverPath "\\chromedriver.exe");

or

WebDriverManager.chromedriver().setup();                
  1. Launch Browser
myDriver= new ChromeDriver();

later you can configure your Tests to run in Local selenium Grid or Cloudbased Cross Browser testing platform (https://www.lambdatest.com/)

  • Related