use*_*317 5 ruby selenium watir sidekiq selenium-webdriver
我有一个关于 Selenium WebDriver 及其在多线程中的使用的问题。我有一个从网站获取一些数据的应用程序,因此我使用使用 Selenium WebDriver 的 Watir 浏览器。我也配置了 Sidekiq。我的流程是我将一些使用 chromedriver 初始化 watir 的作业排入队列,因此同时有 6 个并发线程运行 Selenium WebDriver。有时我会注意到一些错误,例如:
Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9519or
Selenium::WebDriver::Error::WebDriverError: unable to bind to locking port 9514 within 45 seconds或Selenium::WebDriver::Error::UnknownError: unknown error: failed to start a thread for the new session。这与 Selenium 不是线程安全的事实有关吗?我认为如果我在不同的线程中初始化浏览器,那么它应该可以正常工作,但事实并非如此。有没有办法以某种方式修复它?
# frozen_string_literal: true
require 'selenium/webdriver/remote/http/curb'
module Scrapers
class Browser
def run
Watir::Browser.new(:chrome, watir_options)
end
private
def chrome_args
[
'--ignore-certificate-errors',
'--disable-popup-blocking',
'--no-sandbox',
'--window-size=1024,768',
'--disable-extensions'
]
end
def chrome_options
chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
chrome_bin ? { 'chromeOptions' => { 'binary' => chrome_bin } } : {}
end
def watir_options
{
headless: true,
timeout: 60,
http_client: Selenium::WebDriver::Remote::Http::Curb.new,
args: chrome_args
}.merge!(chrome_options)
end
end
end
Run Code Online (Sandbox Code Playgroud)
当调用run方法或对象已经初始化并调用时,它通常会失败goto。
| 归档时间: |
|
| 查看次数: |
581 次 |
| 最近记录: |