Class: BlackStack::Bots::Browser
- Inherits:
-
Watir::Browser
- Object
- Watir::Browser
- BlackStack::Bots::Browser
- Defined in:
- lib/browser.rb
Constant Summary collapse
- LOCKFILENAME =
'/tmp/blackstack.bots.browser.lock'
Instance Attribute Summary collapse
-
#lockfile ⇒ Object
Returns the value of attribute lockfile.
-
#pids ⇒ Object
Returns the value of attribute pids.
Instance Method Summary collapse
-
#close ⇒ Object
initialize.
-
#initialize ⇒ Browser
constructor
A new instance of Browser.
-
#quit ⇒ Object
close.
Constructor Details
#initialize ⇒ Browser
Returns a new instance of Browser.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/browser.rb', line 7 def initialize() self.lockfile = File.open(LOCKFILENAME, 'w+') n = 20 # timeout in seconds # wait the lock file /tmp/blackstack.bots.browser.lock self.lockfile.flock(File::LOCK_EX) begin # get list of PID of all opened chrome browsers, before launching this one pids_before = `pgrep -f chrome`.split("\n") # setup driver client = Selenium::WebDriver::Remote::Http::Default.new begin client.read_timeout = n # for newest selenium webdriver version rescue => e client.timeout = n # deprecated in newest selenium webdriver version end = Selenium::WebDriver::Chrome::Options.new .add_argument('--headless') # setup user agent with-out the keyword "headless" # otherwise, our scraper may be detected as a bot and blocked .add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64)") #+"AppleWebKit/537.36 (KHTML, like Gecko)" #+"Chrome/87.0.4280.141 Safari/537.36") # Add this parameter to run Chrome from a root user. # https://stackoverflow.com/questions/50642308/webdriverexception-unknown-error-devtoolsactiveport-file-doesnt-exist-while-t .add_argument('--no-sandbox') driver = Selenium::WebDriver.for :chrome, :options => , http_client: client # create the browser super(driver) # get list of PID of all opened chrome browsers, after launching this one pids_after = `pgrep -f chrome`.split("\n") # store list of PID regarding this launched browser self.pids = pids_after - pids_before rescue => e # unlock the file self.lockfile.flock(File::LOCK_UN) # raise the error raise e ensure # unlock the file self.lockfile.flock(File::LOCK_UN) end end |
Instance Attribute Details
#lockfile ⇒ Object
Returns the value of attribute lockfile.
5 6 7 |
# File 'lib/browser.rb', line 5 def lockfile @lockfile end |
#pids ⇒ Object
Returns the value of attribute pids.
5 6 7 |
# File 'lib/browser.rb', line 5 def pids @pids end |
Instance Method Details
#close ⇒ Object
initialize
54 55 56 57 |
# File 'lib/browser.rb', line 54 def close # kill all pids regarding this browser self.pids.each { |pid| `kill -9 #{pid} >/dev/null 2>&1` } end |
#quit ⇒ Object
close
59 60 61 62 |
# File 'lib/browser.rb', line 59 def quit # kill all pids regarding this browser self.pids.each { |pid| `kill -9 #{pid} >/dev/null 2>&1` } end |