2012-05-25 53 views

回答

-2

没有我知道的。获取PID通常需要对Selenium代码进行一些修改。但是,您可以从系统中获取PID,而不是从Selenium本身获取。对webdriver的用户群这个话题可能会有所帮助:

https://groups.google.com/forum/#!topic/webdriver/_-8Slyn6qrI

+1

如果有执行,多个浏览器实例并行测试,我们无法选择的过程与如何开始使用它的NodeJS有关硒的会话ID –

17

使用Python API,这是很简单的:

from selenium import webdriver 
browser = webdriver.Firefox() 
print browser.binary.process.pid 
# browser.binary.process is a Popen object... 

如果你使用的浏览器,它更复杂一点,你走通过chromedriver过程:

c = webdriver.Chrome() 
c.service.process # is a Popen instance for the chromedriver process 
import psutil 
p = psutil.Process(c.service.process.pid) 
print p.get_children(recursive=True) 
+1

难道你不知道匹配? – k102

+1

我不知道。我在一个名为'selenium.webdriver.firefox'的模块中通过源代码来挖掘它。也许挖通过js源代码?像这里的某个地方? http://code.google.com/p/selenium/source/browse/javascript/firefox-driver/js/firefoxDriver.js – hwjp

+0

如果您使用PhantomJS,这不起作用。该驱动程序没有二进制属性。 – ABM

8

如果你使用PhantomJS那么你可以从过程POPEN对象得到PID:

from selenium import webdriver 
browser = webdriver.PhantomJS() 
print browser.service.process.pid 
0

对于在这里寻找解决方案的人来说,希望它能帮助你。

protected Integer getFirefoxPid(FirefoxBinary binary){ 
    try { 
     final Field fieldCmdProcess = FirefoxBinary.class.getDeclaredField("process"); 
     fieldCmdProcess.setAccessible(true); 
     final Object ObjCmdProcess = fieldCmdProcess.get(binary); 

     final Field fieldInnerProcess = ObjCmdProcess.getClass().getDeclaredField("process"); 
     fieldInnerProcess.setAccessible(true); 
     final Object objInnerProcess = fieldInnerProcess.get(ObjCmdProcess); 

     final Field fieldWatchDog = objInnerProcess.getClass().getDeclaredField("executeWatchdog"); 
     fieldWatchDog.setAccessible(true); 
     final Object objWatchDog = fieldWatchDog.get(objInnerProcess); 

     final Field fieldReelProcess = objWatchDog.getClass().getDeclaredField("process"); 
     fieldReelProcess.setAccessible(true); 
     final Process process = (Process) fieldReelProcess.get(objWatchDog); 

     final Integer pid; 

     if (Platform.getCurrent().is(WINDOWS)) { 
      final Field f = process.getClass().getDeclaredField("handle"); 
      f.setAccessible(true); 
      long hndl = f.getLong(process); 

      final Kernel32 kernel = Kernel32.INSTANCE; 
      final WinNT.HANDLE handle = new WinNT.HANDLE(); 
      handle.setPointer(Pointer.createConstant(hndl)); 
      pid = kernel.GetProcessId(handle); 

     } else { 
      final Field f = process.getClass().getDeclaredField("pid"); 
      f.setAccessible(true); 
      pid = (Integer) f.get(process); 
     } 
     logger.info("firefox process id : " + pid + " on plateform : " + Platform.getCurrent()); 
     return pid; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     logger.error("Cannot get firefox process id, exception is : {}", e); 
    } 
    return null; 
} 
0

如果您使用的是Java和硒,你可以简单地先找到JVM的PID,然后通过其子进程,你可以得到chromedriver的PID和铬进行类似PID。这里有一个例子来找到chromedriver的PID。

final String jvmName = ManagementFactory.getRuntimeMXBean().getName(); 
    final int index = jvmName.indexOf('@'); 
    if(index > 1) { 
     try { 
      String processId = Long.toString(Long.parseLong(jvmName.substring(0, index))); 
      Scanner scan = new Scanner(Runtime.getRuntime().exec("wmic process where (ParentProcessId="+ processId +") get Caption,ProcessId").getInputStream()); 
      scan.useDelimiter("\\A"); 
      String childProcessIds = scan.hasNext() ? scan.next() : ""; 
      List<String> chromeDrivers = new ArrayList<String>(); 
      String[] splited = childProcessIds.split("\\s+"); 
      for(int i =0 ; i<splited.length; i = i+2){ 
       if("chromedriver.exe".equalsIgnoreCase(splited[i])){ 
        chromeDrivers.add(splited[i+1]); 
       } 
      } 
      /*    
      * 
      *Do whatever you want to do with the chromedriver's PID here  
      * 
      * */   
      scan.close(); 
     } catch (Exception e) { 

     } 
    }