4

To bring browser to front, as per one of comments I need to get PID of browser. This helps to get PID but that is for Python. Is there any way, I can get PID of browser using Selenium WebDriver + Java?

Alpha
  • 13,320
  • 27
  • 96
  • 163

2 Answers2

3

To retrieve the PID of Firefox Browser initiated and controlled by WebDriver using Selenium and you can use getCapabilities() method and you can use the following solution:

  • Code Block:

    import java.io.IOException;
    
    import org.openqa.selenium.Capabilities;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.remote.RemoteWebDriver;
    
    public class PID_Firefox_Java {
    
        public static void main(String[] args) throws IOException {
    
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
            System.out.println("moz:processID value is : "+cap.getCapability("moz:processID"));
        }
    }
    
  • Console Output:

    moz:processID value is : 4576
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    thanks for the answer! It works for Firefox but could not find similar for Chrome. I'm looking for generic solution which will work across browsers. – Alpha Dec 26 '19 at 08:18
1

I'm not sure whether it directly possible for Chrome as @DebanjanB posted for Firefox. You can use below code for now to achieve that

public static void main(String[] args) throws IOException {

    System.out.println(new Example().getProcessId("firefox"));
}


public String getProcessId(String browserName) throws IOException {
    int port = 0;
    WebDriver driver;


    if (browserName.equalsIgnoreCase("firefox")) {
        WebDriverManager.firefoxdriver().setup();
        GeckoDriverService service = GeckoDriverService.createDefaultService();
        driver = new FirefoxDriver(service);
        port = service.getUrl().getPort();


    } else if (browserName.equalsIgnoreCase("chrome")) {
        WebDriverManager.chromedriver().setup();
        ChromeDriverService service = ChromeDriverService.createDefaultService();
        driver = new ChromeDriver(service);
        port = service.getUrl().getPort();

    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("lsof -i :" + port).getInputStream()));
    StringBuilder builder = new StringBuilder();
    String line = null;
    Set<String> set = new HashSet<String>();
    while ((line = reader.readLine()) != null) {
        if (line.contains(String.valueOf(port)) && line.contains("LISTEN")) {
            set.add(line.split(" ")[1]);
        }
    }
    return set.iterator().next();
}

UPDATED:

For windows, command is different. So need to make some tweaks in above code.

BufferedReader reader = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("netstat -aon | find \""+ port+"\"").getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
Set<String> set = new HashSet<String>();
while ((line = reader.readLine()) != null) {
    if (line.contains(String.valueOf(port)) && line.contains("LISTEN")) {
        String s[]=line.split(" ");
        set.add(s[s.length-1]);
    }
}
return set.iterator().next();

Note: The command i passed to get PID is used on Mac. Please change it as per OS in case it doesn't work.

NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • This is resulting into error - `java.io.IOException: Cannot run program "lsof": CreateProcess error=2, The system cannot find the file specified`. I'm working on Windows machine. Can you please suggest how to resolve this? What is equivalent of it? – Alpha Dec 26 '19 at 13:12
  • @TDHM, if you are using windows then it must be a windows command to get a process id using port number. refer https://stackoverflow.com/a/37215252/5097027 – NarendraR Dec 26 '19 at 13:22
  • This returns port correctly but no value is returned for executed command and hence `set.iterator().next();` results into `java.util.NoSuchElementException`. However same command manually executed gives correct result. – Alpha Dec 27 '19 at 06:02
  • @TDHM https://chat.stackoverflow.com/rooms/204920/https-stackoverflow-com-questions-59484118-how-to-get-pid-of-a-browser-instance – NarendraR Dec 27 '19 at 06:58
  • `lsof` takes a long time to run, would it be possible to use something more instantaneous, such as `ps`? – d-b Dec 22 '20 at 10:59