Class: Trail_Selenium
- Inherits:
-
Object
- Object
- Trail_Selenium
- Defined in:
- lib/trail_selenium.rb
Overview
Trail_Selenium
- Authors
-
Mt.Trail
- Version
-
1.0 2016/7/24 Mt.Trail
- Copyright
-
Copyrigth (c) Mt.Trail 2016 All rights reserved.
- License
-
GPL version 2
目的
Seleniumでデータ収集するためのクラス
-
Excelクラスも利用する。
-
Windows用です。
-
パスは‘/’区切りで扱います。‘\’では有りません。
Instance Attribute Summary collapse
-
#driver ⇒ Object
Returns the value of attribute driver.
-
#report_book ⇒ Object
Returns the value of attribute report_book.
-
#report_line_no ⇒ Object
Returns the value of attribute report_line_no.
-
#report_sheet ⇒ Object
Returns the value of attribute report_sheet.
-
#wait ⇒ Object
Returns the value of attribute wait.
Instance Method Summary collapse
-
#add_picture_to_excel(filename, cy, cx, sw, sh, sheet: nil, fit_x: nil, fit_y: nil) ⇒ Object
Excelに画像貼り付け filename : 貼り付ける画像ファイル(Excel内に取り込まれる) cx,cy : 貼り付け位置のカラム(cx)と行(cy) 1始まりの値 sh,sw : 画像の貼り付けドットサイズ 高さ(sh) 幅(sw) sheet : 貼り付けるシートオブジェクト、指定無しの場合@report_sheet fit_x : カラム幅をswに合わせる。 fit_y : 行の高さをshに合わせる。.
-
#close ⇒ Object
Selenium 終了.
-
#disp_msg_array(offset, t = [''], sheet: nil, line_no: nil) ⇒ Object
コンソールへの表示とReportシートへの記録 offset: はコンソール出力時の左マージンとして使用される。 : またExcelシートの場合、何カラム目からデータをセットするかの指定となる。 t : 出力する文字列の配列を指定する。コンソールとExcelシートに出力される。 sheet : デフォルトの@report_sheet以外のシートに出力するときハッシュで指定する。:sheet => other_sheet line_no : 出力の行番号がデフォルトの@report_line_no以外のときハッシュで指定する。 :line_no => 2.
-
#find_element(xp, node: nil) ⇒ Object
エレメント探索 xp : セレクトエレメントを指定するxpath node : 途中の要素からの場合、その要素オブジェクトを指定する :node => element.
-
#find_element_until(xp, node: nil) ⇒ Object
エレメント探索(見つかるまで待つ) xp : セレクトエレメントを指定するxpath node : 途中の要素からの場合、その要素オブジェクトを指定する :node => element.
-
#get_picture(pathname, xp, node: nil, wait_mode: nil, rename: nil) ⇒ Object
xpathで指定された画像エレメント(imgタグ)のURLから画像をファイルに落とす。 node : xpathの開始ノード xp : 画像を指定するxpath pathname : 画像ファイルを書き込むフォルダパス wait_mode: エレメントの出現を待つとき true を指定 :wait_mode=>true rename : ファイル名を元の名前から書き換えるとき指定 :rename => ‘newname.jpg’ : 指定されなければsrc属性に指定されたファイル名が使用される。.
-
#get_picture_via_clipboard(filename, xp, node: nil, wait_mode: nil) ⇒ Object
xpathで指定された画像エレメントから画像をコピー機能を使用し、クリップボード経由でファイルに落とす。 動的に生成される画像を保存するときに使用する。 右クリックで画像をコピーメニューが出ないものには使用できない。.
-
#initialize(wait_time = 10) ⇒ Trail_Selenium
constructor
初期化 wait時間(秒)を指定する。.
-
#login(param) ⇒ Object
ログイン 引数で設定値の配列の配列を渡す、一番最後はsubmitボタンの情報(設定値なし) 各配列要素は下記の形式 [属性名シンボル,属性の値,設定値] 又は [:xpath, ‘xpath指定’,設定値] 例 : login([,[:name,‘Password’,‘password’],]).
-
#open_excel(target, tenplate = '') ⇒ Object
データ書き込み用のExcelを指定 target : openするExcelファイルのパス tenplate : テンプレートのexcelファイルのパス、これをtargetにコピーしてからopenする。.
-
#open_report_sheet(book, sheet_name) ⇒ Object
レポート用のExcelシートのオープン book : openされたexcelオブジェクト sheet_name : シート番号またはシート名.
-
#open_sheet(book, sheet_name) ⇒ Object
Excelシートのオープン book : openされたexcelオブジェクト sheet_name : シート番号またはシート名.
-
#select_by_text(xp, tx) ⇒ Object
セレクトBOX選択 xp : セレクトエレメントを指定するxpath tx : 選択する文字列の内容.
Constructor Details
#initialize(wait_time = 10) ⇒ Trail_Selenium
初期化 wait時間(秒)を指定する。
50 51 52 53 54 55 56 |
# File 'lib/trail_selenium.rb', line 50 def initialize (wait_time = 10) @driver = Selenium::WebDriver.for :firefox @wait = Selenium::WebDriver::Wait.new(:timeout => wait_time) # seconds @report_book = nil @report_sheet = nil @report_line_no = 1 end |
Instance Attribute Details
#driver ⇒ Object
Returns the value of attribute driver.
46 47 48 |
# File 'lib/trail_selenium.rb', line 46 def driver @driver end |
#report_book ⇒ Object
Returns the value of attribute report_book.
47 48 49 |
# File 'lib/trail_selenium.rb', line 47 def report_book @report_book end |
#report_line_no ⇒ Object
Returns the value of attribute report_line_no.
47 48 49 |
# File 'lib/trail_selenium.rb', line 47 def report_line_no @report_line_no end |
#report_sheet ⇒ Object
Returns the value of attribute report_sheet.
47 48 49 |
# File 'lib/trail_selenium.rb', line 47 def report_sheet @report_sheet end |
#wait ⇒ Object
Returns the value of attribute wait.
46 47 48 |
# File 'lib/trail_selenium.rb', line 46 def wait @wait end |
Instance Method Details
#add_picture_to_excel(filename, cy, cx, sw, sh, sheet: nil, fit_x: nil, fit_y: nil) ⇒ Object
Excelに画像貼り付け
filename : 貼り付ける画像ファイル(Excel内に取り込まれる)
cx,cy : 貼り付け位置のカラム(cx)と行(cy) 1始まりの値
sh,sw : 画像の貼り付けドットサイズ 高さ(sh) 幅(sw)
sheet : 貼り付けるシートオブジェクト、指定無しの場合@report_sheet
fit_x : カラム幅をswに合わせる。
fit_y : 行の高さをshに合わせる。
244 245 246 247 248 249 250 251 252 |
# File 'lib/trail_selenium.rb', line 244 def add_picture_to_excel(filename,cy,cx,sw,sh,sheet: nil,fit_x: nil,fit_y: nil) sheet = @report_sheet if ! sheet if sheet r = sheet.Range(sheet.r_str(cy,cx)) sheet.Shapes.AddPicture(filename.gsub("\/","\\"),false,true, r.Left.to_i, r.Top.to_i, 0.75*sw, 0.75*sh) sheet.set_width(cy,cx,0.118*sw) if fit_x sheet.set_height(cy,cx,0.75*sh) if fit_y end end |
#close ⇒ Object
Selenium 終了
305 306 307 |
# File 'lib/trail_selenium.rb', line 305 def close @driver.quit() end |
#disp_msg_array(offset, t = [''], sheet: nil, line_no: nil) ⇒ Object
コンソールへの表示とReportシートへの記録
offset: はコンソール出力時の左マージンとして使用される。
: またExcelシートの場合、何カラム目からデータをセットするかの指定となる。
t : 出力する文字列の配列を指定する。コンソールとExcelシートに出力される。
sheet : デフォルトの@report_sheet以外のシートに出力するときハッシュで指定する。:sheet => other_sheet
line_no : 出力の行番号がデフォルトの@report_line_no以外のときハッシュで指定する。 :line_no => 2
出力するExcelシートと出力行はハッシュで指定する。指定されない場合、最後にopen_report_sheetで開いたシートが使われる。
出力する行は指定されない場合 @report_line_noが使用される。
出力後はsheetが指定されていない場合 @report_line_noは + 1 される。
出力文字列にカンマ等を含まないという制限条件はあるがoffset=0のコンソール出力をファイルにリダイレクトするとCSVファイルとなる。
注意 : Excelへ出力する場合open_excelのブロック内で利用されなければならない。
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/trail_selenium.rb', line 151 def disp_msg_array(offset,t=[''],sheet: nil,line_no: nil ) print ' '*offset + t.map{|x| x.to_s}.join(', ') + "\n" sheet = @report_sheet if !sheet line = @report_line_no if !line_no if sheet t.each_with_index do |tt,i| sheet[line, offset+i+1] = tt end @report_line_no += 1 if !line_no end end |
#find_element(xp, node: nil) ⇒ Object
エレメント探索
xp : セレクトエレメントを指定するxpath
node : 途中の要素からの場合、その要素オブジェクトを指定する :node => element
見つからないときにはnilを返す。
271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/trail_selenium.rb', line 271 def find_element(xp,node: nil) begin if node link = node.find_element(:xpath,xp) else link = @driver.find_element(:xpath,xp) end rescue link = nil end link end |
#find_element_until(xp, node: nil) ⇒ Object
エレメント探索(見つかるまで待つ)
xp : セレクトエレメントを指定するxpath
node : 途中の要素からの場合、その要素オブジェクトを指定する :node => element
見つからないときにはnilを返す。
290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/trail_selenium.rb', line 290 def find_element_until(xp,node: nil) begin if node link = @wait.until{node.find_element(:xpath,xp)} else link = @wait.until{@driver.find_element(:xpath,xp)} end rescue link = nil end link end |
#get_picture(pathname, xp, node: nil, wait_mode: nil, rename: nil) ⇒ Object
xpathで指定された画像エレメント(imgタグ)のURLから画像をファイルに落とす。
node : xpathの開始ノード
xp : 画像を指定するxpath
pathname : 画像ファイルを書き込むフォルダパス
wait_mode: エレメントの出現を待つとき true を指定 :wait_mode=>true
rename : ファイル名を元の名前から書き換えるとき指定 :rename => 'newname.jpg'
: 指定されなければsrc属性に指定されたファイル名が使用される。
<return> : 画像ファイルパス or nil
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/trail_selenium.rb', line 206 def get_picture(pathname,xp,node: nil,wait_mode:nil,rename:nil) if wait_mode img = find_element_until(xp,:node => node) else img = find_element(xp,:node => node) end savefile = nil if img pathname += '/' if (pathname != '') and (pathname[-1] != '/') url = img[:src] if rename savefile = pathname + rename else filename = File.basename(url) savefile = pathname + filename end open(savefile,'wb') do |wf| open(url) do |rf| wf.write( rf.read ) end end end savefile end |
#get_picture_via_clipboard(filename, xp, node: nil, wait_mode: nil) ⇒ Object
xpathで指定された画像エレメントから画像をコピー機能を使用し、クリップボード経由でファイルに落とす。
動的に生成される画像を保存するときに使用する。 右クリックで画像をコピーメニューが出ないものには使用できない。
node : xpathの開始ノード
xp : 画像を指定するxpath
filename : 書き込む画像ファイル名
wait_mode: エレメントの出現を待つとき true を指定 :wait_mode=>true
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/trail_selenium.rb', line 176 def get_picture_via_clipboard(filename,xp,node: nil,wait_mode:nil) if wait_mode img = find_element_until(xp,:node => node) else img = find_element(xp,:node => node) end if img @driver.action.context_click(img).send_keys('Y').perform if Win32::Clipboard.format_available?(Win32::Clipboard::DIB) File.open(filename,'wb') do |f| f.write Win32::Clipboard.data(Win32::Clipboard::DIB) end end end img end |
#login(param) ⇒ Object
ログイン
引数で設定値の配列の配列を渡す、一番最後はsubmitボタンの情報(設定値なし)
各配列要素は下記の形式
[属性名シンボル,属性の値,設定値] 又は [:xpath, 'xpath指定',設定値]
例 : login([[:name,'UserName','LoginName'],[:name,'Password','password'],[:name,'Submit']])
64 65 66 67 68 69 70 71 72 |
# File 'lib/trail_selenium.rb', line 64 def login (param) param.each_with_index do |p,i| if i < (param.size - 1) @driver.find_element(p[0], p[1]).send_keys(p[2]) else @driver.find_element(p[0], p[1]).click end end end |
#open_excel(target, tenplate = '') ⇒ Object
データ書き込み用のExcelを指定
target : openするExcelファイルのパス
tenplate : テンプレートのexcelファイルのパス、これをtargetにコピーしてからopenする。
テンプレートを指定するとそれをコピーして使用する。
ブロックで処理内容を受け取る
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/trail_selenium.rb', line 84 def open_excel (target,tenplate = '') @target_excel = target if tenplate != '' FileUtils.cp( tenplate, @target_excel) end openExcelWorkbook(@target_excel) do |book| @report_book = book yield book end end |
#open_report_sheet(book, sheet_name) ⇒ Object
レポート用のExcelシートのオープン
book : openされたexcelオブジェクト
sheet_name : シート番号またはシート名
@report_sheetを設定する Excelに書き込む場合、こちらを指定すると書き込み関数呼び出し時にパラメータを減らせる。
128 129 130 131 |
# File 'lib/trail_selenium.rb', line 128 def open_report_sheet( book, sheet_name ) @report_sheet = open_sheet( book, sheet_name ) @report_sheet end |
#open_sheet(book, sheet_name) ⇒ Object
Excelシートのオープン
book : openされたexcelオブジェクト
sheet_name : シート番号またはシート名
ブックとシート名を指定する。
シート名が数値の場合シートの番号と見なされる
Excelのシートオブジェクトを返す
例
ts = Trail_Selenium.new
ts.open_excel('report.xls','report_tenplate.xls') do |book|
sheet = open_report_sheet(book,'Report_Sheet')
:
book.save
end
115 116 117 118 119 |
# File 'lib/trail_selenium.rb', line 115 def open_sheet( book, sheet_name ) sheet = book.Worksheets.Item(sheet_name) sheet.extend Worksheet sheet end |
#select_by_text(xp, tx) ⇒ Object
セレクトBOX選択
xp : セレクトエレメントを指定するxpath
tx : 選択する文字列の内容
259 260 261 262 |
# File 'lib/trail_selenium.rb', line 259 def select_by_text(xp,tx) select = Selenium::WebDriver::Support::Select.new( @wait.until{@driver.find_element(:xpath,xp)} ) select.select_by(:text,tx.encode('UTF-8')) end |