-1

When using this class in Selenium WebDriver:

package Selenium3;

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Jobs {

static WebDriver driver;

public Jobs(WebDriver driver) {
    this.driver = driver;
}

public static void con() throws InterruptedException {
    List<WebElement> element = driver.findElements(By.cssSelector(".position_title.ng-binding"));
    for (int i = 0; i < element.size(); i++) {
        Thread.sleep(2000);
        String u = element.get(i).getText();
        if (u.contains("Java"));
        System.out.println(u);
    }
  }
}

The driver is always null. In the end I want to call this method from my Main class. What am I doing wrong?

duggu
  • 37,851
  • 12
  • 116
  • 113

1 Answers1

3

You have an static method (that not requires class instantiation)... so when you call the method without instantiating the class you wont initialize your driver:

You have various solutions:

  • Make method not static (you will force initialization of driver with constructor, but you can reuse the instantiated class various times)

    public class Jobs {
    
        private WebDriver driver;
    
        public Jobs(WebDriver driver) {
           this.driver = driver;
        }
    
        public void con() throws InterruptedException {
            List<WebElement> element = driver.findElements(By.cssSelector(".position_title.ng-binding"));
            for (int i = 0; i < element.size(); i++) {
                Thread.sleep(2000);
                String u = element.get(i).getText();
                if (u.contains("Java"));
                System.out.println(u);
            }
    }
    
  • Keep method static adding driver as a parameter (so you will have your driver prior to call method and you won't need to instantiate the class)

    public class Jobs {
        public static void con(WebDriver driver) throws InterruptedException {
            List<WebElement> element = driver.findElements(By.cssSelector(".position_title.ng-binding"));
            for (int i = 0; i < element.size(); i++) {
                Thread.sleep(2000);
                String u = element.get(i).getText();
                if (u.contains("Java"));
                System.out.println(u);
            }
    }
    
  • Use a static block to initialize the driver prior the method static call (faster to call, but problem is you cannot chuoose which driver implementation you will use)

    public class Jobs {
    
        static WebDriver driver;
    
        static {
           this.driver = new FirefoxDriver(); // just an example
        }
    
        public static void con() throws InterruptedException {
            List<WebElement> element = driver.findElements(By.cssSelector(".position_title.ng-binding"));
            for (int i = 0; i < element.size(); i++) {
                Thread.sleep(2000);
                String u = element.get(i).getText();
                if (u.contains("Java"));
                System.out.println(u);
            }
    }
    
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 1
    `WebDriver` is an interface. Change to `new FirefoxDriver();` the line with comment :) – Fenio Feb 15 '18 at 11:37
  • 1
    thanks @RafałLaskowski :) you make me think about a new option – Jordi Castilla Feb 15 '18 at 11:44
  • Hi @assaf-sagee as you're a new user and you never marked a question as answered I think you must know if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. Of course, there is no obligation to do this. – Jordi Castilla Feb 19 '18 at 11:57