LoginSignup
2
0

More than 5 years have passed since last update.

Headless Chrome + Capybara で何故か要素が取得できない場合、Headless Chrome のウインドウサイズを大きくすると取得できることがある

Posted at

page.source には要素が入っているのに、Capybara から取得しようとすると何故か取得できないことがあって。

色々試して Headless Chrome のウインドウサイズ内に描画されていないことが判明。以下のように大きめのウインドウサイズを設定することで無事に要素が取得できるようになりました。めでたしめでたし。

diff --git a/spec/turnip_helper.rb b/spec/turnip_helper.rb
index b308ed5f..d931e5a2 100644
--- a/spec/turnip_helper.rb
+++ b/spec/turnip_helper.rb
@@ -13,7 +13,7 @@ end

 Capybara.register_driver :headless_chrome do |app|
   capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
-    chromeOptions: {args: %w(headless disable-gpu)}
+    chromeOptions: {args: %w(headless disable-gpu window-size=1920,1080)}
   )
   Capybara::Selenium::Driver.new(
     app,
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0