[英]Google login with mechanize on ruby
我正在嘗試使用 ruby 訪問google play 開發者控制台。 但首先我必須登錄。 我正在嘗試這樣:
def try_post(url, body = {}, headers = {})
unless @agent #This just creates a new mechanize instance
setup
end
puts 'Logging in'
# Hardcoded for testing purposes
@agent.get 'https://accounts.google.com/ServiceLogin?service=androiddeveloper&passive=1209600&continue=https://play.google.com/apps/publish/%23&followup=https://play.google.com/apps/publish/#identifier'
form = @agent.page.forms.find {|f| f.form_node['id'] == "gaia_loginform"}
unless form
raise 'No login form'
end
form.field_with(:id => "Email").value = @config.email
form.click_button
form = @agent.page.forms.find {|f| f.form_node['id'] == "gaia_loginform"}
unless form
raise 'No login form'
end
form.field_with(:name => "Passwd").value = @config.password
form.click_button
if @agent.page.uri.host != "play.google.com"
STDERR.puts "login failed? : uri = " + @agent.page.uri.to_s
raise 'Google login failed'
end
# @agent.post(url, body)
end
然而,這失敗了。 我嘗試了其他一些方法(嘗試填充Passwd-hidden
,通過id
查找字段等)但沒有運氣。 我認為沒有輸入密碼,因為當我嘗試在最后的click_button
之后puts @agent.page.body
,我看到在 HTML 中的某處輸入密碼文本。
我做錯了什么,我該如何解決?
我一直在挖掘更多,發現它並不那么簡單,我無法以任何方式使用機械化登錄。
所以我最終使用了相當簡單明了的watir 。 下面是一個例子:
browser.goto LOGIN_URL
browser.text_field(:id, 'Email').set @config.email
browser.button(:id, 'next').click
browser.text_field(:id, 'Passwd').wait_until_present
browser.text_field(:id, 'Passwd').set @config.password
browser.button(:id, 'signIn').click
# Here I wait until an element on my target page is visible and then continue
browser.link(:href, '#SOMETHING').wait_until_present
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.