5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Seleniumでタイムアウトを設定/要素が読み込まれるまで待つ方法 (ruby)

Last updated at Posted at 2016-04-02

Seleniumでタイムアウト

任意の要素が読み込まれるまで待った後に処理を行い、指定した時間内にその要素が見つからない場合にはタイムアウトする場合。

require 'selenium-webdriver'

# ブラウザとwait(10秒)のインスタンスを生成
$browser = Selenium::WebDriver.for :firefox, profile: default_profile
$wait = Selenium::WebDriver::Wait.new(timeout: 10)

$browser.navigate.to 'https://www.hogehoge'

begin
  #id_nameをidに持つ要素が見つかるまでwait, 10秒の間に見つからなければTimeout
  @element = $wait.until { $browser.find_element(id: id_name) }
rescue RuntimeError => e
  print e.message
  $browser.close
rescue => e
  print e.message + "\n"
end

# click
@element.click

最後に

自分への勉強も込めて書いてみました。誰かの参考になれば幸いです。

また、個人的に電車などで使える場所で起こす目覚ましアプリMapMeとNetflixの映画レビューサイトWhatchaSeeを運営しているので、使っていただけるとありがたいです。

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?