Class: FerrumWizard
- Inherits:
-
Object
- Object
- FerrumWizard
- Includes:
- RXFHelperModule
- Defined in:
- lib/ferrumwizard.rb
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
-
#buttons ⇒ Object
readonly
Returns the value of attribute buttons.
-
#js_methods ⇒ Object
readonly
Returns the value of attribute js_methods.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#radio ⇒ Object
readonly
Returns the value of attribute radio.
Instance Method Summary collapse
- #at_xpath(s) ⇒ Object
- #body ⇒ Object
- #click(xpath) ⇒ Object
-
#initialize(url = nil, headless: true, timeout: 10, cookies: nil, debug: false) ⇒ FerrumWizard
constructor
A new instance of FerrumWizard.
- #input(xpath, value) ⇒ Object
- #inspect ⇒ Object
-
#load_cookies(raws) ⇒ Object
(also: #loadx)
Intended to load all the cookies for a user to login automatically.
- #login(usernamex = nil, passwordx = nil, username: usernamex, password: passwordx) ⇒ Object
-
#login2(usernamex = nil, passwordx = nil, username: usernamex, password: passwordx) ⇒ Object
login2 is used for websites where the user is presented with the username input box on the first page and the password input box on the next page.
- #quit ⇒ Object
-
#save_cookies(filepath = Tempfile.new('ferrum').path) ⇒ Object
Saves all cookies for a given website into a YAML file see also load_cookies().
- #scan_page ⇒ Object
-
#select(xpath, title) ⇒ Object
selects an item from a nested drop-down list.
-
#select_down(e, n) ⇒ Object
presses the down arrow n number of times to select a drop-down list item.
- #submit(h) ⇒ Object
- #to_doc ⇒ Object
- #to_rb ⇒ Object
Constructor Details
#initialize(url = nil, headless: true, timeout: 10, cookies: nil, debug: false) ⇒ FerrumWizard
Returns a new instance of FerrumWizard.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ferrumwizard.rb', line 16 def initialize(url=nil, headless: true, timeout: 10, cookies: nil, debug: false) @url, @debug = url, debug @browser = Ferrum::Browser.new headless: headless, timeout: timeout loadx() if if url then sleep 3 @browser.goto(@url) @browser.network.wait_for_idle sleep 4 end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object (private)
417 418 419 420 421 422 423 |
# File 'lib/ferrumwizard.rb', line 417 def method_missing(method_name, *args) puts 'method_missing: ' + method_name.inspect if @debug node = @browser.at_css '.' + method_name.to_s node.text if node end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
14 15 16 |
# File 'lib/ferrumwizard.rb', line 14 def browser @browser end |
#buttons ⇒ Object (readonly)
Returns the value of attribute buttons.
14 15 16 |
# File 'lib/ferrumwizard.rb', line 14 def @buttons end |
#js_methods ⇒ Object (readonly)
Returns the value of attribute js_methods.
14 15 16 |
# File 'lib/ferrumwizard.rb', line 14 def js_methods @js_methods end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
14 15 16 |
# File 'lib/ferrumwizard.rb', line 14 def links @links end |
#radio ⇒ Object (readonly)
Returns the value of attribute radio.
14 15 16 |
# File 'lib/ferrumwizard.rb', line 14 def radio @radio end |
Instance Method Details
#at_xpath(s) ⇒ Object
35 36 37 |
# File 'lib/ferrumwizard.rb', line 35 def at_xpath(s) @browser.at_xpath(s) end |
#body ⇒ Object
39 40 41 |
# File 'lib/ferrumwizard.rb', line 39 def body() @browser.body end |
#click(xpath) ⇒ Object
43 44 45 46 47 |
# File 'lib/ferrumwizard.rb', line 43 def click(xpath) r = @browser.at_xpath(xpath) r.focus.click sleep 0.4 end |
#input(xpath, value) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ferrumwizard.rb', line 49 def input(xpath, value) r = @browser.at_xpath(xpath) puts 'xpath: ' + xpath.inspect if @debug raise 'invalid xpath -> ' + xpath.inspect unless r sleep 1 if @debug r.focus.type value sleep 1 end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/ferrumwizard.rb', line 60 def inspect() "#<FerrumWizard>" end |
#load_cookies(raws) ⇒ Object Also known as: loadx
Intended to load all the cookies for a user to login automatically
Follow these steps to load the cookies file:
1. launch the Ferrum browser
fw = FerrumWizard.new( headless: false, debug: false)
2. load the before you visit the website
fw.('/tmp/indeed2.txt')
3. visit the website
url='https://somewebsite.com'
fw.browser.goto(url)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ferrumwizard.rb', line 78 def (raws) s = raws.lines.length > 1 ? raws : FileX.read(raws) puts 's: ' + s.inspect if @debug = YAML.load(s) .each do |h| if @debug then puts 'name: ' + h['name'] puts 'h: ' + h.inspect sleep 0.1 end browser..set(name: h['name'], value: h['value'], domain: h['domain'], expires: h['expires'], httponly: h['httpOnly']) end end |
#login(usernamex = nil, passwordx = nil, username: usernamex, password: passwordx) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ferrumwizard.rb', line 102 def login(usernamex=nil, passwordx=nil, username: usernamex, password: passwordx) puts 'username: ' + username.inspect if @debug # search for the username input box e_username = @browser.at_xpath('//input[@type="email"]') puts 'e_username: ' + e_username.inspect if @debug sleep 1 # search for the password input box found = @browser.at_xpath('//input[@type="password"]') e_password = if found then found else @browser.xpath('//input').find {|x| x.property(:id) =~ /password/i} end sleep 1 if username and e_username then puts 'entering the username' if @debug e_username.focus.type(username) sleep 1 end e_password.focus.type(password, :Enter) if e_password after_login() end |
#login2(usernamex = nil, passwordx = nil, username: usernamex, password: passwordx) ⇒ Object
login2 is used for websites where the user is presented with the username input box on the first page and the password input box on the next page.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ferrumwizard.rb', line 136 def login2(usernamex=nil, passwordx=nil, username: usernamex, password: passwordx) puts 'username: ' + username.inspect if @debug # search for the username input box e_username = @browser.at_xpath('//input[@type="email"]') puts 'e_username: ' + e_username.inspect if @debug sleep 1 # search for the password input box if username and e_username then puts 'entering the username' if @debug e_username.focus.type(username, :Enter) sleep 2 end e_password = @browser.at_xpath('//input[@type="password"]') sleep 1 e_password.focus.type(password, :Enter) if e_password after_login() end |
#quit ⇒ Object
162 163 164 |
# File 'lib/ferrumwizard.rb', line 162 def quit @browser.quit end |
#save_cookies(filepath = Tempfile.new('ferrum').path) ⇒ Object
Saves all cookies for a given website into a YAML file see also load_cookies()
To use this method follow these steps:
1. launch the web browser through Ferrum
fw = FerrumWizard.new(url, headless: false, debug: false)
2. go to the browser and login using your credentials
fw.(filepath)
3. exit the IRB session
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/ferrumwizard.rb', line 189 def (filepath=Tempfile.new('ferrum').path) = @browser..all.keys.map do |key| if @debug then puts 'key: ' + key.inspect sleep 0.5 end s = @browser.[key].inspect a = s.scan(/"([^"]+)"=\>/) s2 = s[/(?<=@attributes=).*(?=>)/] eval(s2) end File.write filepath, .to_yaml end |
#scan_page ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/ferrumwizard.rb', line 166 def scan_page() @doc = Rexle.new @browser.body fetch_links() scan_form_elements() scan_js_links() @browser.mouse.scroll_to(0, 800) self end |
#select(xpath, title) ⇒ Object
selects an item from a nested drop-down list
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/ferrumwizard.rb', line 211 def select(xpath, title) e = @browser.at_xpath(xpath) raise 'invalid xpath -> ' + xpath.inspect unless e titles = if e.tag_name == 'select' then e.xpath('option').map(&:text) else e.xpath('select/option').map(&:text) end r = titles.grep /#{title}/i n = titles.index(r.first) select_down(e, n) sleep 0.5 end |
#select_down(e, n) ⇒ Object
presses the down arrow n number of times to select a drop-down list item
231 232 233 234 235 |
# File 'lib/ferrumwizard.rb', line 231 def select_down(e, n) e.focus n.times { e.type(:down); sleep 0.4} e.click end |
#submit(h) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/ferrumwizard.rb', line 237 def submit(h) e = nil h.each do |key, value| e = @browser.xpath('//input').find {|x| x.attribute('name') == key.to_s} e.focus.type(value) end e.focus.type('', :Enter) sleep 4 scan_page() end |
#to_doc ⇒ Object
253 254 255 |
# File 'lib/ferrumwizard.rb', line 253 def to_doc() @doc end |
#to_rb ⇒ Object
257 258 |
# File 'lib/ferrumwizard.rb', line 257 def to_rb() end |