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
- #body ⇒ Object
-
#initialize(url = nil, headless: true, timeout: 10, cookies: nil, debug: false) ⇒ FerrumWizard
constructor
A new instance of FerrumWizard.
- #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
- #submit(h) ⇒ 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)
364 365 366 367 368 369 370 |
# File 'lib/ferrumwizard.rb', line 364 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 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
#body ⇒ Object
35 36 37 |
# File 'lib/ferrumwizard.rb', line 35 def body() @browser.body end |
#inspect ⇒ Object
39 40 41 |
# File 'lib/ferrumwizard.rb', line 39 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)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ferrumwizard.rb', line 57 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.4 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ferrumwizard.rb', line 81 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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ferrumwizard.rb', line 115 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
141 142 143 |
# File 'lib/ferrumwizard.rb', line 141 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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/ferrumwizard.rb', line 168 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
145 146 147 148 149 150 151 152 153 |
# File 'lib/ferrumwizard.rb', line 145 def scan_page() @doc = Rexle.new @browser.body fetch_links() scan_form_elements() scan_js_links() @browser.mouse.scroll_to(0, 800) self end |
#submit(h) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/ferrumwizard.rb', line 188 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_rb ⇒ Object
204 205 |
# File 'lib/ferrumwizard.rb', line 204 def to_rb() end |