javascript - casperjs (phantomjs) 网站更新后无法提交表单

标签 javascript jquery forms phantomjs

直到最近网站更新后,我的脚本才运行了一段时间。

https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry

我所做的就是填写表格,单击“提交”并抓取结果。

现在看来,无论我做什么,单击,发送回车键......等等,表单都不会提交。

我有来自 casper 的以下调试行:

casperjs casper1.js
[info] [phantom] Starting...
[info] [phantom] Running suite: 4 steps
[debug] [phantom] opening url: https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry, HTTP GET
[debug] [phantom] Navigation requested: url=https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/4 https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry (HTTP 200)
[info] [phantom] Step anonymous 2/4: done in 1579ms.
[info] [phantom] Step _step 3/5 https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry (HTTP 200)
[info] [phantom] Step _step 3/5: done in 1599ms.
[info] [phantom] waitFor() finished in 40ms.
[info] [phantom] Step anonymous 4/5 https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry (HTTP 200)
[debug] [phantom] Mouse event 'mousedown' on selector: form#form1
[debug] [phantom] Mouse event 'mouseup' on selector: form#form1
[debug] [phantom] Mouse event 'click' on selector: form#form1
[info] [phantom] Step anonymous 4/5: done in 1691ms.
[info] [phantom] Step anonymous 5/5 https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry (HTTP 200)
[debug] [phantom] Capturing page to /var/www/html/poc/phantom/test.png
[info] [phantom] Capture saved to /var/www/html/poc/phantom/test.png
[info] [phantom] Step anonymous 5/5: done in 1964ms.
[info] [phantom] Done 5 steps in 1967ms
[debug] [phantom] Capturing page to /var/www/html/poc/phantom/test0.png
[info] [phantom] Capture saved to /var/www/html/poc/phantom/test0.png
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"
<小时/>
var casper = require('casper').create({
    verbose: true,
    userAgent: 'Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36',
    XSSAuditingEnabled: false,
    logLevel: 'debug',
    javascriptEnabled:'false',
    viewportSize: {
            width: 1280,
        height: 720
    },
    pageSettings: {
        ignoreSslErrors: true,
        loadImages: false, // do not load images
        loadPlugins: false, // do not load NPAPI plugins (Flash, Silverlight, ...)
        webSecurityEnabled: false,
        localToRemoteUrlAccessEnabled: false
    }
});

casper.start('https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry', function() {
   // Wait for the form
   this.waitForSelector('form#form1');
});

casper.then(function() {
   // fill the form
   this.fill('form#form1', { "ph_pagebody_0$phthreecolumnmaincontent_0$panel$VehicleSearch$RegistrationNumberCar$RegistrationNumber_CtrlHolderDivShown": "ZJD402" }, false);
   this.click('form#form1');
});

casper.then(function() {
        this.capture('test.png');
});

casper.run(function() {
    // dump
    this.capture('test0.png');
    //this.debugPage();
    this.exit();
});
<小时/>

我也尝试过等待包含结果的选择器,但它从未到达。

任何 phantomjs/casperjs 大师都有什么妙招吗?

最佳答案

您点击的不是提交按钮,而是整个表单。

this.click('form#form1');

处方集不是按钮,因此不是“可点击的”。您应该寻找提交按钮并单击它。在这种情况下,我这样做是正确的:

this.click('"#main input[type='submit']"');

就是这样。

使用'form#form1',您实际上是在寻找submit,但我不认为直接调用submit表单将触发附加到按钮的 onClick 事件,因此如果您想 100% 模拟用户操作,最好单击按钮而不是直接提交表单。

要直接提交表单,只需删除 fill 方法末尾的 false 属性即可。

关于javascript - casperjs (phantomjs) 网站更新后无法提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45319326/

相关文章:

javascript - easeljs 棕褐色和灰度

javascript - 如何使用jquery注入(inject)带有 Angular Directive(指令)的html元素?

javascript - 变量的计算结果始终为 false

javascript - 使用 jQuery 更改选择选项时更改占位符

javascript - 固定顶部导航栏的变体

javascript - 如何多次重用一个 Angular 组件

javascript - 如何在 Angular 5中同步调用http请求

javascript - 如果只有一个选项可用,则禁用选择

javascript - 如何为 AngularJS 表单验证要求至少 1 个复选框?

javascript - 每个 jquery AJAX 发布一个可变长度的表单