【问题标题】:CasperJS: How to fill a asp formCasperJS:如何填写 asp 表单
【发布时间】:2015-01-29 06:38:56
【问题描述】:

我正在尝试通过 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 都会捕获初始页面的屏幕截图。非常感谢任何帮助..谢谢

【问题讨论】:

    标签: javascript asp.net xpath phantomjs casperjs


    【解决方案1】:

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

    似乎 PhantomJS 在默认情况下是在禁用 XSLT 处理的情况下编译的。因此,样式表没有被处理,PhantomJS 将其视为任意数据。

    您可以使用所需的选项集编译 PhantomJS,也可以使用 SlimerJS 作为 CasperJS 的引擎。使用的 Gecko 引擎默认支持 XSLT 转换。

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

    【讨论】:

    • 请注意,如果你有 xvfb,SlimerJS 也可以运行headlessly
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 2016-09-06
    • 1970-01-01
    相关资源
    最近更新 更多