Get PID of Browser launched by selenium

前端 未结 9 1578
闹比i
闹比i 2020-11-30 07:15

I would like to get the PID of the browser launched by selenium. Is there any way to get it done?

9条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 07:54

    In Java, if you use ChromeDriver, you can find the port that the driver will use

    port = chromeDriverService.getUrl().getPort();
    

    and then, using the port, you can find the chromedriver process id by running the command

    netstat -anp | grep LISTEN | grep [port] (on linux)
    

    or

    netstat -aon | findstr LISTENING | findstr [port] (on windows)
    

    You can go further, to find out the chrome process id, by using the chromedriver process id (parent id of the chrome process)

    ps -efj | grep google-chrome | grep [chromedriverprocessid] (on linux)
    

    or

    wmic process get processid,parentprocessid,executablepath | find \"chrome.exe\" |find \"chromeDriverProcessID\"
    

    the code looks like this:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Level;
    
    import org.apache.commons.lang.SystemUtils;
    import org.openqa.selenium.WebDriverException;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeDriverService;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.logging.LogType;
    import org.openqa.selenium.logging.LoggingPreferences;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    public class WebdriverProcessID
    {
      public static void main(String[] args) throws IOException, InterruptedException
      {
        ChromeDriver driver = null;
    
        ChromeOptions options = new ChromeOptions();
        List listArguments = new ArrayList();
    
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(ChromeOptions.CAPABILITY, options);
    
        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
        cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
    
        ChromeDriverService chromeDriverService = ChromeDriverService.createDefaultService();
        int port = chromeDriverService.getUrl().getPort();
    
        driver = new ChromeDriver(chromeDriverService, cap);
    
        System.out.println("starting chromedriver on port " + port);
        int chromeDriverProcessID = GetChromeDriverProcessID(port);
        System.out.println("detected chromedriver process id " + chromeDriverProcessID);
        System.out.println("detected chrome process id " + GetChromeProcesID(chromeDriverProcessID));
    
        driver.navigate().to("https://www.test.com/");
    
        try
        {
          Thread.sleep(100000);
        }
        catch (InterruptedException e)
        {
        }
    
        try
        {
          driver.close();
        }
        catch (WebDriverException ex)
        {
          ex.printStackTrace();
        }
    
        try
        {
          driver.quit();
        }
        catch (WebDriverException ex)
        {
          ex.printStackTrace();
        }
      }
    
      private static int GetChromeDriverProcessID(int aPort) throws IOException, InterruptedException
      {
        String[] commandArray = new String[3];
    
        if (SystemUtils.IS_OS_LINUX)
        {
          commandArray[0] = "/bin/sh";
          commandArray[1] = "-c";
          commandArray[2] = "netstat -anp | grep LISTEN | grep " + aPort;
        }
        else if (SystemUtils.IS_OS_WINDOWS)
        {
          commandArray[0] = "cmd";
          commandArray[1] = "/c";
          commandArray[2] = "netstat -aon | findstr LISTENING | findstr " + aPort;
        }
        else
        {
          System.out.println("platform not supported");
          System.exit(-1);
        }
    
        System.out.println("running command " + commandArray[2]);
    
        Process p = Runtime.getRuntime().exec(commandArray);
        p.waitFor();
    
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
        StringBuilder sb = new StringBuilder();
        String line = "";
        while ((line = reader.readLine()) != null)
        {
          sb.append(line + "\n");
        }
    
        String result = sb.toString().trim();
    
        System.out.println("parse command response line:");
        System.out.println(result);
    
        return SystemUtils.IS_OS_LINUX ? ParseChromeDriverLinux(result) : ParseChromeDriverWindows(result);
      }
    
      private static int GetChromeProcesID(int chromeDriverProcessID) throws IOException, InterruptedException
      {
        String[] commandArray = new String[3];
    
        if (SystemUtils.IS_OS_LINUX)
        {
          commandArray[0] = "/bin/sh";
          commandArray[1] = "-c";
          commandArray[2] = "ps -efj | grep google-chrome | grep " + chromeDriverProcessID;
        }
        else if (SystemUtils.IS_OS_WINDOWS)
        {
          commandArray[0] = "cmd";
          commandArray[1] = "/c";
          commandArray[2] = "wmic process get processid,parentprocessid,executablepath | find \"chrome.exe\" |find \"" + chromeDriverProcessID + "\"";
        }
        else
        {
          System.out.println("platform not supported");
          System.exit(-1);
        }
    
        System.out.println("running command " + commandArray[2]);
    
        Process p = Runtime.getRuntime().exec(commandArray);
        p.waitFor();
    
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
        StringBuilder sb = new StringBuilder();
        String line = "";
        while ((line = reader.readLine()) != null)
        {
          if (SystemUtils.IS_OS_LINUX && line.contains("/bin/sh"))
          {
            continue;
          }
    
          sb.append(line + "\n");
        }
    
        String result = sb.toString().trim();
    
        System.out.println("parse command response line:");
        System.out.println(result);
    
        return SystemUtils.IS_OS_LINUX ? ParseChromeLinux(result) : ParseChromeWindows(result);
      }
    
      private static int ParseChromeLinux(String result)
      {
        String[] pieces = result.split("\\s+");
        // root 20780 20772 20759 15980  9 11:04 pts/1    00:00:00 /opt/google/chrome/google-chrome.........
        // the second one is the chrome process id
        return Integer.parseInt(pieces[1]);
      }
    
      private static int ParseChromeWindows(String result)
      {
        String[] pieces = result.split("\\s+");
        // C:\Program Files (x86)\Google\Chrome\Application\chrome.exe 14304 19960
        return Integer.parseInt(pieces[pieces.length - 1]);
      }
    
      private static int ParseChromeDriverLinux(String netstatResult)
      {
        String[] pieces = netstatResult.split("\\s+");
        String last = pieces[pieces.length - 1];
        // tcp 0 0 127.0.0.1:2391 0.0.0.0:* LISTEN 3333/chromedriver
        return Integer.parseInt(last.substring(0, last.indexOf('/')));
      }
    
      private static int ParseChromeDriverWindows(String netstatResult)
      {
        String[] pieces = netstatResult.split("\\s+");
        // TCP 127.0.0.1:26599 0.0.0.0:0 LISTENING 22828
        return Integer.parseInt(pieces[pieces.length - 1]);
      }
    }
    

    the output will be, on linux:

    starting chromedriver on port 17132
    running command netstat -anp | grep LISTEN | grep 17132
    parse command response line:
    tcp        0      0 127.0.0.1:17132         0.0.0.0:*               LISTEN      22197/chromedriver
    detected chromedriver process id 22197
    running command ps -efj | grep google-chrome | grep 22197
    parse command response line:
    root     22204 22197 22183 15980 26 11:17 pts/1    00:00:00 /opt/google/chrome/google-chrome ...
    detected chrome process id 22204
    

    and on windows:

    starting chromedriver on port 34231
    running command netstat -aon | findstr LISTENING | findstr 34231
    parse command response line:
    TCP    127.0.0.1:34231        0.0.0.0:0              LISTENING       10692
    detected chromedriver process id 10692
    running command wmic process get "processid,parentprocessid,executablepath" | findstr "chrome.exe" | findstr "10692"
    parse command response line:
    C:\Program Files (x86)\Google\Chrome\Application\chrome.exe  10692 12264
    detected chrome process id 12264
    

提交回复
热议问题