Seleniumでスクレイピングするときに「is not clickable at point (772, 590). Other element would receive the click:」的なものが出る問題
エラー発生
Seleniumでスクレイピングをする際にheadlessのオプションを追加すると以下のようなエラーが出てくる。
product_src_url_and_img_url_hash[:src_url]: https://~~. Error: unknown error: Element <a href="#" id="ms_001" style="outline:none;" title="...(M)">36</a> is not clickable at point (772, 590). Other element would receive the click: <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja" dir="ltr" xmlns:fb="http://ogp.me/ns/fb#" class="mac chrome chrome7 webkit webkit5">...</html> (Session info: headless chrome=70.0.3538.77) (Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.14.0 x86_64).
解決策
結論から言うとウインドウのサイズを指定するオプションを追加すれば良い。
ぐぐってるうちに以下のサイトに遭遇。
headless chromeは、poltergeistと比べて「ブラウザ上で見える要素であるか否か」にシビアなようです。例えば、position: fixed; left: 1000px としているDOM要素があるとします。このとき、ブラウザのウィンドウサイズが(800, 600)のように小さく要素が画面外になる場合は、その要素は見えないという扱いになります。
ん?単純に見えないならウインドウのサイズを指定してあげたらいいんじゃないか?と思い、以下のようにしてみたら無事解決されました。
def selenium_and_capybara_config options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless') options.add_argument('--window-size=1080,1080') # ここを追加する Capybara.configure do |capybara_config| capybara_config.default_driver = :selenium capybara_config.default_max_wait_time = 10 end Capybara.register_driver :selenium do |app| Capybara::Selenium::Driver.new(app, browser: :chrome, options: options) end # 今まではoptionsを付けてスクレイピングすると上記エラーが発生していた # Selenium::WebDriver.for :chrome Selenium::WebDriver.for :chrome, options: options end
無事ブラウザが表示されずにスクレイピングを行うことが出来た。
因みに、 --window-size=800,800
にしてみたところ同じエラーが発生した。
ある程度の大きさが無いとダメっぽい。