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
-
#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)
360 361 362 363 364 365 366 |
# File 'lib/ferrumwizard.rb', line 360 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
#inspect ⇒ Object
35 36 37 |
# File 'lib/ferrumwizard.rb', line 35 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)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ferrumwizard.rb', line 53 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
77 78 79 80 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 |
# File 'lib/ferrumwizard.rb', line 77 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.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ferrumwizard.rb', line 111 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
137 138 139 |
# File 'lib/ferrumwizard.rb', line 137 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
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ferrumwizard.rb', line 164 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
141 142 143 144 145 146 147 148 149 |
# File 'lib/ferrumwizard.rb', line 141 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
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/ferrumwizard.rb', line 184 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
200 201 |
# File 'lib/ferrumwizard.rb', line 200 def to_rb() end |