2012-10-17 2 views
2

Mechanize를 사용하여 로그인 양식을 제출할 때 문제가 발생했습니다. 예를 들어, 내가의 bitbucket에 로그인해야하는 경우 : 꽤 똑바로 앞으로, 그러나 모든 로그인 폼이 두 필드에 같은 "이름"값을 가질 것mechanize-ruby를 사용하여 로그인 양식을 제출할 때 변수를 사용하여 필드 이름을 나타낼 수 있습니까?

a = Mechanize.new 
a.get('https://bitbucket.org/') do |page| 
    login_page = a.click(page.link_with(text: 'Log In')) 

    my_page = login_page.form_with(action: '/account/signin/') do |f| 
    # The "username" and "password" below are the values of the "name" attribute of the two login form fields 
    f.username = 'MY_ACCOUNT_NAME' 
    f.password = 'MY_PASSWORD' 
    end.click_button 
end 

. 예를 들어 WordPress의 로그인 양식은 "log"및 "pwd"를 사용합니다. 위의 코드는 무효화됩니다.

다른 로그인 양식에서 사용할 수 있도록 일부 매개 변수를이 메서드에 전달하고 싶습니다. 나는 "How to convert from a string to object attribute name?"를 따르도록 시도했지만 실패했습니다 : 누군가가 도울 수 있다면

# auth_info is a hash 
def website_login(auth_info) 
    a = Mechanize.new 
    a.get(auth_info[:login_page_url]) do |page| 
    land_page = page.form_with(action: auth_info[:login_form_action]) do |f| 
     # Now I am stuck with these two lines 
     ???????? 
     ???????? 
     # I tried to do it like this. Not working. 
     f.instance_variable_set('@'+auth_info[:username_field_name], auth_info[:username]) 
     f.instance_variable_set('@'+auth_info[:password_field_name], auth_info[:password]) 
    end.click_button 
    end 
end 

정말 감사드립니다.

+0

"해결 된"을 제목에 추가하지 마십시오. 답안을 작성하고 그 답안지를 입력하십시오. Stack Overflow를 사용하면 유예 기간이 끝난 후 해답을 솔루션으로 선택할 수 있으므로 질문에 자동으로 응답 할 수 있습니다. –

+0

@ theTinMan 고맙습니다. – blacktulip

답변

0

해결되었습니다. 다음과 같습니다.

f.send(auth_info[:username_field_name] + '=', auth_info[:username]) 
f.send(auth_info[:password_field_name] + '=', auth_info[:password]) 
관련 문제