| | 103 | |
| | 104 | - 检测登录论坛登录例子 |
| | 105 | {{{ |
| | 106 | require 'rubygems' |
| | 107 | require 'selenium-webdriver' |
| | 108 | require 'iconv' |
| | 109 | class String |
| | 110 | def to_gbk |
| | 111 | Iconv.iconv("GBK//IGNORE","UTF-8//IGNORE",self).to_s |
| | 112 | end |
| | 113 | end |
| | 114 | |
| | 115 | |
| | 116 | driver = Selenium::WebDriver.for :ie |
| | 117 | driver.navigate.to "http://itbbs.pconline.com.cn/mobile/f587012.html" |
| | 118 | #sleep 3 |
| | 119 | |
| | 120 | #输入帐号密码 |
| | 121 | username = driver.find_element(:name, 'username') |
| | 122 | username.send_keys "3edc4rfv" |
| | 123 | password = driver.find_element(:name, 'password') |
| | 124 | password.send_keys "123456" |
| | 125 | password.submit #提交数据 |
| | 126 | |
| | 127 | #读取cookie查看是否登录成功 |
| | 128 | driver.manage.all_cookies.each { |cookie| |
| | 129 | puts "pass!" if cookie[:name] == "cmu" and cookie[:value] == "3edc4rfv" |
| | 130 | #puts "#{cookie[:name]} => #{cookie[:value]}" |
| | 131 | } |
| | 132 | #puts "pass!" if driver.manage.cookie["cmu"] == "3edc4rfv" |
| | 133 | driver.manage.delete_all_cookies #清除cookie |
| | 134 | #puts "#{driver.title}".to_gbk |
| | 135 | driver.quit |
| | 136 | }}} |