Class: Qzone::Login
- Inherits:
-
Object
- Object
- Qzone::Login
- Defined in:
- lib/qzone/qzone_login.rb
Instance Method Summary collapse
- #close ⇒ Object
- #cookies ⇒ Object
- #gtk ⇒ Object
-
#initialize(user, password, wait_time) ⇒ Login
constructor
A new instance of Login.
- #login ⇒ Object
Constructor Details
#initialize(user, password, wait_time) ⇒ Login
Returns a new instance of Login.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/qzone/qzone_login.rb', line 9 def initialize user, password, wait_time @user = user @password = password begin user_agent = 'User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50' capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs( 'phantomjs.page.settings.userAgent' => user_agent, "phantomhs.page.settings.loadImages" => false ) @driver = Selenium::WebDriver.for(:phantomjs, :desired_capabilities => capabilities) @waiter = Selenium::WebDriver::Wait.new(timeout: wait_time) rescue raise 'init error,make sure phantomjs has been installed correctly.' end end |
Instance Method Details
#close ⇒ Object
95 96 97 |
# File 'lib/qzone/qzone_login.rb', line 95 def close @driver.close if @driver end |
#cookies ⇒ Object
91 92 93 |
# File 'lib/qzone/qzone_login.rb', line 91 def @driver.manage. end |
#gtk ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/qzone/qzone_login.rb', line 81 def gtk # 获取gtk @skey = @driver.manage. 'p_skey' hashes = 5381 @skey[:value].each_char do |c| hashes += (hashes << 5) + c.ord end hashes & 0x7fffffff end |
#login ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/qzone/qzone_login.rb', line 27 def login @waiter.until {@driver.navigate.to 'http://qzone.qq.com/'} @waiter.until {@driver.switch_to.frame 'login_frame'} @waiter.until { @driver.find_element(id: 'switcher_plogin') } plogin = @driver.find_element(id: 'switcher_plogin') plogin.click user = @driver.find_element(id: 'u') user.send_keys @user password = @driver.find_element(id: 'p') password.send_keys @password @waiter.until { @driver.find_element(xpath: '//*[@id="login_button"]') } login = @driver.find_element(xpath: '//*[@id="login_button"]') login.click sleep(1) # 如果正常的话 目前就已经返回true return @pass = true if @driver.current_url != 'http://qzone.qq.com/' # 下面分析可能的原因 begin @waiter.until {@driver.find_element(id: 'vcode')} raise 'Need verify code, please manualy login Qzone first' rescue Exception => error if error. == 'Need verify code, please manualy login Qzone first' raise error. end end # if find element with id "err_m", indicate something wrong begin @waiter.until {@driver.find_element(id: 'err_m')} err = @driver.find_element(id: 'err_m') = err.text rescue # if can't find that element, indicate something wrong happened but -> # we don't know about that. raise 'Unknown error happened in login' end if .include? '您输入的帐号或密码不正确' raise 'Account error' elsif .include? '网络繁忙' raise 'Network busy, try later' else raise 'Unknown error happened in login' end end |