我想获得由硒启动的浏览器的PID。有什么办法可以完成它吗?获取由硒启动的浏览器的PID
19
A
回答
-2
没有我知道的。获取PID通常需要对Selenium代码进行一些修改。但是,您可以从系统中获取PID,而不是从Selenium本身获取。对webdriver的用户群这个话题可能会有所帮助:
https://groups.google.com/forum/#!topic/webdriver/_-8Slyn6qrI
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)
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) {
}
}
相关问题
- 1. 启动Firefox浏览器中的硒
- 2. 在硒中启动浏览器
- 3. Maven是不会启动浏览器硒
- 4. 如何启动浏览器在硒
- 5. 无浏览器启动 - 硒的webdriver - java.lang.NoClassDefFoundError:COM /谷歌/ GSON/JsonElement
- 6. 无法启动使用硒任何浏览器3.0的webdriver
- 7. 如何启动硒浏览器并手动关闭它?
- 8. 获取当前打开的浏览器窗口在硒2(WebDriver)
- 9. python中的硒webdriver - 无法获取浏览器
- 10. 获取浏览器
- 11. 无法启动IE浏览器在硒webdriver
- 12. 如何用代理启动硒浏览器
- 13. 无法启动任何浏览器使用硒webdriver
- 14. 无法通过SSH启动硒浏览器
- 15. 如何使用硒网格启动多个浏览器?
- 16. 如何使用硒启动Chrome浏览器
- 17. Jenkins&TestNG启动浏览器
- 18. 启动ClojureScript浏览器REPL
- 19. Phonegap:启动Firefox浏览器
- 20. 启动精简浏览器
- 21. 启动Chrome浏览器
- 22. 启动android浏览器
- 23. 启动浏览器(Safari浏览器)我的应用程序
- 24. 硒浏览器何时自动死亡?
- 25. 捕获由Makefile启动的后台进程的PID
- 26. 由于“浏览器”中的URL链接而启动JavaScript函数
- 27. 如何在硒测试中捕获浏览器启动链接请求?
- 28. 如何获取Webview iframe链接来启动浏览器?
- 29. 是否有可能在Windows XP中获取由exec()启动的进程的PID?
- 30. 获取浏览器滚动高度
如果有执行,多个浏览器实例并行测试,我们无法选择的过程与如何开始使用它的NodeJS有关硒的会话ID –