finding the login form based on content

2019-08-19 07:24发布

Basically I want to find out the login form when there are multiple forms on a HTML document. My guess is as following: What if i can identify to which form strings like 'forgot your password' or 'Remember me' or 'keep me signed in' belongs then it might be login form since such strings/link appear after the password field.

Is there any way i can find out which form contains these strings/links and then use that form 's name or ID in my app ?

Update: I used following code to get form ID which contains strings 'forgot your password' but it returns nothing.

NSString *formID = [NSString stringWithFormat:@"%@",[self.browser stringByEvaluatingJavaScriptFromString:@"$('form').filter (function() {var text = this.innerHTML.toLowerCase();" 
                        "return text.indexOf('forgot your password') != -1 || "
                        "text.indexOf('remember me') != -1;}).attr('id');"] ];

2条回答
唯我独甜
2楼-- · 2019-08-19 08:05
var loginForm;
$("form").each(function(){
    if($(this).text().indexOf("Remember me")>=0)
         loginForm = $(this);
})
查看更多
地球回转人心会变
3楼-- · 2019-08-19 08:18
var loginFormId = $('form').filter(function() {
    return this.innerHTML.indexOf('forgot your password') != -1;
}).attr('id');​

Or simply with :contains:

var loginFormId = $('form:contains("forgot your password")').attr('id');​

:contains selector docs:

Description: Select all elements that contain the specified text. The matching text can appear directly within the selected element, in any of that element's descendants, or a combination thereof. As with attribute value selectors, text inside the parentheses of :contains() can be written as a bare word or surrounded by quotation marks. The text must have matching case to be selected.

LIVE DEMO


Update:

If you are not sure about the case of the text, you must use filter:

var theFormId = $('form').filter(function() {
    var text = this.innerHTML.toLowerCase(); 
    return text.indexOf('forgot your password') != -1 || 
           text.indexOf('remember me') != -1;
查看更多
登录 后发表回答