CasperJS:如何填写asp表单

CasperJS: How to fill a asp form

本文关键字:asp 表单 何填写 CasperJS      更新时间:2023-09-26

我正在尝试通过casperjs自动化asp编写网站的几个步骤。我想实现以下目标。

  1. 导航到登录页面
  2. 填写用户名和密码字段
  3. 单击登录按钮
  4. 捕获成功页面的屏幕截图

但到目前为止,我只能完成该过程的第一步。源视图页面对我来说看起来很少。大多数节点都以<ui:>标签开头,我对此一无所知。

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="theme/xslt/all.xsl?build=2.0.012&amp;theme=theme_immi" ?> 
<!DOCTYPE html [ <!ENTITY nbsp "&#160;"> ]> 
<ui:root title="ImmiAccount - Login" xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:ui="http://www.immi.gov.au/Namespace/UI/V1.0"> 

我首先尝试fillSelectors()提交表单的方法。

casper.start('https://online.immi.gov.au/lusc/login',function() {
    this.echo(this.getTitle());
    this.fillSelectors('#app_L2',{
        'input[id="app_L2b0a0a0a3a1a"]':  'john',
        'input[id="app_L2b0a0a0a3b1a"]':  'john123'
    });
    this.wait('2000',function(){
        this.capture('formfill.png');
    });    
});
casper.then(function() {
    // Click on 1st result link
    this.click('#app_L2b0a0a0a4a');
    this.wait('5000',function(){
        this.capture('success.png');
    })
});

出现以下错误

CasperError: Errors encountered while filling form: no field matching css selector "input[id="app_L2b0a0a0a3a1a"]" in form; 
no field matching css selector "input[id="app_L2b0a0a0a3b1a"]" in form

然后尝试了fillXPath()

casper.start('https://online.immi.gov.au/lusc/login',function() {
    this.echo(this.getTitle());
    this.fillXPath('#app_L2',{
        '//*[@id="app_L2b0a0a0a3a1a"]':    'john',
        '//*[@id="app_L2b0a0a0a3b1a"]':  'john123'
    });
    this.wait('2000',function(){
        this.capture('formfill.png');
    });    
});
casper.then(function() {
    // Click on 1st result link
    this.click(x('//*[@id="app_L2b0a0a0a4a"]'),
    this.wait('5000',function(){
        this.capture('success.png');
    }));
});

以下错误发生

CasperError: Errors encountered while filling form: Unable to find field element in form: FieldNotFound: Invalid field type; only HTMLElement and NodeList are supported; Unable to find field element in form: FieldNotFound: Invalid field type; only HTMLElement and NodeList are supported

最后,我尝试sendkeys()方法来填写字段并click()点击登录按钮。再次,没有找到成功。这里的代码这样做

this.sendKeys('#app_L2b0a0a0a3a1a', 'john');
this.sendKeys('#app_L2b0a0a0a3b1a', 'john123');
this.click(x('//*[@id="app_L2b0a0a0a4a"]'),
  this.wait('5000',function(){
    this.capture('ctrlqpass.png');
  })
);

上面的代码既不会触发错误,也不会触发成功页面。在所有情况下,casper 都会捕获初始页面的屏幕截图。任何帮助非常感谢。谢谢

该页面是一个带有附加样式表 (XSL) 的 XML 文档,用于将页面转换为 HTML 或 XHTML。这是由大多数浏览器自动完成的。

似乎 PhantomJS 是默认编译的,禁用了 XSLT 处理。因此,样式表不会被处理,PhantomJS将其视为任意数据。

你可以使用所需的选项集编译PhantomJS,也可以使用SlimerJS作为CasperJS的引擎。默认情况下,使用的壁虎引擎确实支持 XSLT 转换。

另一种途径可能是使用 JavaScript 中包含的 XSLTProcessor 自己转换内容,但这在 PhantomJS 中也不可用。