Extract pid of browser running Selenium WebDriver in Ruby

Does anyone know how to get the process id of the browser launched by Selenium WebDriver from the Ruby script that runs WebDriver?

+4
source share
3 answers

Ben's answer did not help me, I had to adapt it to the following:

driver = Selenium::WebDriver.for :chrome bridge = driver.instance_variable_get(:@bridge) service = bridge.instance_variable_get(:@service) process = service.instance_variable_get(:@process) process.pid # => 22656 
+5
source
 require "selenium-webdriver" driver = Selenium::WebDriver.for :firefox bridge = driver.instance_variable_get(:@bridge) launcher = bridge.instance_variable_get(:@launcher) binary = launcher.instance_variable_get(:@binary) process = binary.instance_variable_get(:@process) process.pid 
+2
source

Both answers did not work for me, because it is part of a private api.
Checkout on github

 require "selenium-webdriver" # gem 3.9.0 driver = Selenium::WebDriver.for :firefox pid = driver.instance_variable_get(:@service) .instance_variable_get(:@process) .instance_variable_get(:@pid) 




Note:

For those trying to access the browser using pid, for example. to execute the kill -9 $pid command. This may be wrong because I came across a better solution.

In args commands we can pass custom attrs. I use it as follows

 @buid = SecureRandom.hex[0..15] # browser unique identifier options = Selenium::WebDriver::Firefox::Options.new( args: [ '-headless', "-buid=#{@buid}", ] ) 

So you can grep and kill both browsers and geckodriver. By exec

 $ps aux | awk '/-buid=$generated_pid/ {print $2}' | xargs kill -9 

Hope this helps!

+2
source

Source: https://habr.com/ru/post/1393454/


All Articles