使用PhantomJS的Selenium:表单已验证但未提交 [英] Selenium with PhantomJS: Form being validated but not submitted

查看:74
本文介绍了使用PhantomJS的Selenium:表单已验证但未提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过Selenium Webdriver的PhantomJS API提交表单时遇到了一个奇怪的问题.单击提交按钮后,表单将得到验证(用户名和密码太短或为空等),但最终并没有提交.也就是说,如果我提交了无效的表格并检查了屏幕截图,则会有警报通知.如果我提交有效的表格,则不会发生任何事情.页面上的JS应该先验证表单,然后单击提交"按钮,然后提交表单.

两次淘汰.我在提交按钮(按钮元素)上尝试了.click().submit().我还尝试过.submit()在表单本身上以及在Selenium允许的表单中的任意元素(例如密码)上.

实际上,如果我使用的是Firefox,而不是PhantomJS,则相同的代码也可以正常工作.不过,我宁愿不进行切换,因为Firefox速度较慢,并且给我带来了无法预测的连接问题.

我的规格:我在Ubuntu 14.04 LTS(GNU/Linux 3.17.1-elastic x86_64)上通过Python 2.7使用Selenium 1.43和PhantomJS 1.98.

下面的代码.首先是我的硒代码.然后是表单的html.然后是该站点的login.js源代码.我认为js中发生的情况是由于某种原因,正在运行.validate函数中的invalidHandler,但是没有运行submitHandler吗?预先感谢您的关注.

# enter username and password up here first
submitEl = self.find_element_by_css_selector("button[type='submit']")
submitEl.click()
self.save_screenshot('login_submission.png')

Edit2:"self"是从Webdriver.PhantomJS类继承的驱动程序对象.现在是html格式:

            <form class="form-login" action="" name="login" method="POST">
                <div class="errorHandler alert alert-danger no-display">
                    <i class="fa fa-remove-sign"></i> You have some form errors. Please check below.
                </div>
                                    <fieldset>
                    <div class="form-group">
                        <span class="input-icon">
                            <input type="text" class="form-control" name="username" placeholder="Username">
                            <i class="fa fa-user"></i> </span>
                    </div>
                    <div class="form-group form-actions">
                        <span class="input-icon">
                            <input type="password" class="form-control password" name="password" placeholder="Password">
                            <i class="fa fa-lock"></i>
                            <a class="forgot" href="forgot.php">
                                I forgot my password
                            </a> </span>
                    </div>
                    <div class="form-group">
                    <img src="captcha/captcha.php" alt="captcha" />
                        <span class="input-icon" style="width:200px; float: right;">
                            <input type="text" class="form-control" name="captcha">
                            <i class="fa fa-key"></i> </span>
                    </div>
                    <div class="form-actions" ><div class="slideExpandUp">
                        <label for="remember" class="checkbox-inline">
                            <input type="checkbox" class="grey remember" id="remember" name="remember">
                            Keep me signed in
                        </label>
                        <button type="submit" class="btn btn-bricky pull-right" name="submit">
                            Login <i class="fa fa-arrow-circle-right"></i>
                        </button></div>
                    </div>
                    <div class="new-account">
                        Don't have an account yet?
                        <a href="register.php" class="register">
                            Create an account
                        </a>
                    </div>
                </fieldset>
            </form>

login.js

var Login = function () {
    var runSetDefaultValidation = function () {
        $.validator.setDefaults({
            errorElement: "span", // contain the error msg in a small tag
            errorClass: 'help-block',
            errorPlacement: function (error, element) { // render error placement for each input type
                if (element.attr("type") == "radio" || element.attr("type") == "checkbox") { // for chosen elements, need to insert the error after the chosen container
                    error.insertAfter($(element).closest('.form-group').children('div').children().last());
                } else if (element.attr("name") == "card_expiry_mm" || element.attr("name") == "card_expiry_yyyy") {
                    error.appendTo($(element).closest('.form-group').children('div'));
                } else {
                    error.insertAfter(element);
                    // for other inputs, just perform default behavior
                }
            },
            ignore: ':hidden',
            highlight: function (element) {
                $(element).closest('.help-block').removeClass('valid');
                // display OK icon
                $(element).closest('.form-group').removeClass('has-success').addClass('has-error').find('.symbol').removeClass('ok').addClass('required');
                // add the Bootstrap error class to the control group
            },
            unhighlight: function (element) { // revert the change done by hightlight
                $(element).closest('.form-group').removeClass('has-error');
                // set error class to the control group
            },
            success: function (label, element) {
                label.addClass('help-block valid');
                // mark the current input as valid and display OK icon
                $(element).closest('.form-group').removeClass('has-error');
            },
            highlight: function (element) {
                $(element).closest('.help-block').removeClass('valid');
                // display OK icon
                $(element).closest('.form-group').addClass('has-error');
                // add the Bootstrap error class to the control group
            },
            unhighlight: function (element) { // revert the change done by hightlight
                $(element).closest('.form-group').removeClass('has-error');
                // set error class to the control group
            }
        });
    };
    var runLoginValidator = function () {
        var form = $('.form-login');
        var errorHandler = $('.errorHandler', form);
        form.validate({
            rules: {
                username: {
                    minlength: 2,
                    required: true
                },
                password: {
                    minlength: 6,
                    required: true
                }
            },
            submitHandler: function (form) {
                errorHandler.hide();
                form.submit();
            },
            invalidHandler: function (event, validator) { //display error alert on form submit
                errorHandler.show();
            }
        });
    };
    return {
        //main function to initiate template pages
        init: function () {
            runSetDefaultValidation();
            runLoginValidator();
        }
    };
}();

固定标题

PhantomJS日志文件输出,根据请求:

PhantomJS is launching GhostDriver...
[INFO  - 2015-01-27T16:58:04.367Z] GhostDriver - Main - running on port 48152
[INFO  - 2015-01-27T16:58:05.366Z] Session [a9641420-a645-11e4-95fd-c78c9ec356b6] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0","webSecurityEnabled":true}
[INFO  - 2015-01-27T16:58:05.366Z] Session [a9641420-a645-11e4-95fd-c78c9ec356b6] - page.customHeaders:  - {}
[INFO  - 2015-01-27T16:58:05.366Z] Session [a9641420-a645-11e4-95fd-c78c9ec356b6] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"1.9.8","driverName":"ghostdriver","driverVersion":"1.1.0","platform":"linux-unknown-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"},"phantomjs.page.settings.userAgent":"Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0"}
[INFO  - 2015-01-27T16:58:05.366Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: a9641420-a645-11e4-95fd-c78c9ec356b6
[ERROR - 2015-01-27T16:59:08.083Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1422377948079

最后的ERROR不是我的问题的一部分,而是更多的书挡.当抓取算法在登录后应显示在页面上的链接上时,就会发生这种情况.由于我们未成功提交登录表单,因此该链接自然不存在.

解决方案

我不确定我的哪些更改导致了突破,但是我可以说我添加了以下代码.其中只有两个是新的.删除了一个.

    webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.page.settings.localToRemoteUrlAccessEnabled"] = True
    webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.page.settings.browserConnectionEnabled"] = True

,我自己提交了表单,而不是单击按钮(在更改上述配置之前,我也曾尝试过此操作).

        formEl = self.find_element_by_css_selector("form[name='login']")
        formEl.submit()

I'm having a strange problem submitting a form through Selenium Webdriver's PhantomJS API. Upon clicking the submit button, the form gets validated (are the username and password too short, or blank, etc.), but it does not get ultimately submitted. That is, if I submit an invalid form, and check the screenshot, there are alert notifications. If I submit a valid form, nothing happens. The JS on the page is supposed to validate the form, then submit it, when the submit button is clicked.

A couple rule-outs. I've tried both .click() and .submit() on the submit button, which is a button element. I've also tried .submit() on the form itself and on an arbitrary element (like password) within the form, which Selenium allows.

In fact, the same code works just fine if I'm driving Firefox and not PhantomJS. I'd rather not switch over, though, as Firefox is slower and gives me unpredictable connection trouble.

My specs: I'm using Selenium version 1.43 with PhantomJS 1.98 via Python 2.7 on Ubuntu 14.04 LTS (GNU/Linux 3.17.1-elastic x86_64).

Code below. First is my Selenium code. Then the html for the form. Then the login.js source code from the site. What I think is happening in the js is that for some reason the invalidHandler from the .validate function is being run, but the submitHandler is not? Thanks in advance for your attention.

# enter username and password up here first
submitEl = self.find_element_by_css_selector("button[type='submit']")
submitEl.click()
self.save_screenshot('login_submission.png')

Edit2: "self" is a driver object that inherits from the Webdriver.PhantomJS class. Now the form html:

            <form class="form-login" action="" name="login" method="POST">
                <div class="errorHandler alert alert-danger no-display">
                    <i class="fa fa-remove-sign"></i> You have some form errors. Please check below.
                </div>
                                    <fieldset>
                    <div class="form-group">
                        <span class="input-icon">
                            <input type="text" class="form-control" name="username" placeholder="Username">
                            <i class="fa fa-user"></i> </span>
                    </div>
                    <div class="form-group form-actions">
                        <span class="input-icon">
                            <input type="password" class="form-control password" name="password" placeholder="Password">
                            <i class="fa fa-lock"></i>
                            <a class="forgot" href="forgot.php">
                                I forgot my password
                            </a> </span>
                    </div>
                    <div class="form-group">
                    <img src="captcha/captcha.php" alt="captcha" />
                        <span class="input-icon" style="width:200px; float: right;">
                            <input type="text" class="form-control" name="captcha">
                            <i class="fa fa-key"></i> </span>
                    </div>
                    <div class="form-actions" ><div class="slideExpandUp">
                        <label for="remember" class="checkbox-inline">
                            <input type="checkbox" class="grey remember" id="remember" name="remember">
                            Keep me signed in
                        </label>
                        <button type="submit" class="btn btn-bricky pull-right" name="submit">
                            Login <i class="fa fa-arrow-circle-right"></i>
                        </button></div>
                    </div>
                    <div class="new-account">
                        Don't have an account yet?
                        <a href="register.php" class="register">
                            Create an account
                        </a>
                    </div>
                </fieldset>
            </form>

login.js

var Login = function () {
    var runSetDefaultValidation = function () {
        $.validator.setDefaults({
            errorElement: "span", // contain the error msg in a small tag
            errorClass: 'help-block',
            errorPlacement: function (error, element) { // render error placement for each input type
                if (element.attr("type") == "radio" || element.attr("type") == "checkbox") { // for chosen elements, need to insert the error after the chosen container
                    error.insertAfter($(element).closest('.form-group').children('div').children().last());
                } else if (element.attr("name") == "card_expiry_mm" || element.attr("name") == "card_expiry_yyyy") {
                    error.appendTo($(element).closest('.form-group').children('div'));
                } else {
                    error.insertAfter(element);
                    // for other inputs, just perform default behavior
                }
            },
            ignore: ':hidden',
            highlight: function (element) {
                $(element).closest('.help-block').removeClass('valid');
                // display OK icon
                $(element).closest('.form-group').removeClass('has-success').addClass('has-error').find('.symbol').removeClass('ok').addClass('required');
                // add the Bootstrap error class to the control group
            },
            unhighlight: function (element) { // revert the change done by hightlight
                $(element).closest('.form-group').removeClass('has-error');
                // set error class to the control group
            },
            success: function (label, element) {
                label.addClass('help-block valid');
                // mark the current input as valid and display OK icon
                $(element).closest('.form-group').removeClass('has-error');
            },
            highlight: function (element) {
                $(element).closest('.help-block').removeClass('valid');
                // display OK icon
                $(element).closest('.form-group').addClass('has-error');
                // add the Bootstrap error class to the control group
            },
            unhighlight: function (element) { // revert the change done by hightlight
                $(element).closest('.form-group').removeClass('has-error');
                // set error class to the control group
            }
        });
    };
    var runLoginValidator = function () {
        var form = $('.form-login');
        var errorHandler = $('.errorHandler', form);
        form.validate({
            rules: {
                username: {
                    minlength: 2,
                    required: true
                },
                password: {
                    minlength: 6,
                    required: true
                }
            },
            submitHandler: function (form) {
                errorHandler.hide();
                form.submit();
            },
            invalidHandler: function (event, validator) { //display error alert on form submit
                errorHandler.show();
            }
        });
    };
    return {
        //main function to initiate template pages
        init: function () {
            runSetDefaultValidation();
            runLoginValidator();
        }
    };
}();

Edit: fixed the title

PhantomJS log file output, by request:

PhantomJS is launching GhostDriver...
[INFO  - 2015-01-27T16:58:04.367Z] GhostDriver - Main - running on port 48152
[INFO  - 2015-01-27T16:58:05.366Z] Session [a9641420-a645-11e4-95fd-c78c9ec356b6] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0","webSecurityEnabled":true}
[INFO  - 2015-01-27T16:58:05.366Z] Session [a9641420-a645-11e4-95fd-c78c9ec356b6] - page.customHeaders:  - {}
[INFO  - 2015-01-27T16:58:05.366Z] Session [a9641420-a645-11e4-95fd-c78c9ec356b6] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"1.9.8","driverName":"ghostdriver","driverVersion":"1.1.0","platform":"linux-unknown-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"},"phantomjs.page.settings.userAgent":"Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0"}
[INFO  - 2015-01-27T16:58:05.366Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: a9641420-a645-11e4-95fd-c78c9ec356b6
[ERROR - 2015-01-27T16:59:08.083Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1422377948079

The ERROR at the end is not part of my problem, but more of a bookend here. It occurs when the scraping algorithm looks for a link that's supposed to be on the page that shows up after logging in. Since we didn't successfully submit the login form, that link is naturally not present.

解决方案

i'm not exactly certain which of my changes caused the breakthrough, but i can say i added the following code. edit: only two of these were new. removed one.

    webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.page.settings.localToRemoteUrlAccessEnabled"] = True
    webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.page.settings.browserConnectionEnabled"] = True

and i submitted the form itself, rather than clicking a button ( i had also tried this previously, prior to changing the above configs ).

        formEl = self.find_element_by_css_selector("form[name='login']")
        formEl.submit()

这篇关于使用PhantomJS的Selenium:表单已验证但未提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆