如何使用casperjs浏览html选项卡

时间:2014-06-25 01:13:52

标签: html css-selectors casperjs

我需要你在casperjs的经验! 我正在尝试访问网页(这不是问题)并浏览html选项卡。 当我访问页面时,它默认情况下始终显示第一个选项卡“常规”,但我需要切换到“选项”选项卡,以便我可以访问一个我有兴趣修改该值的字段!

抱歉,无法发布图片!

Html代码:

<b>&nbsp;<b>
    <table class="commonfont" cellpadding="0" cellspacing="0" background="/tab_between.png" border="0">
        <tbody>
            <tr>
              <td><input name="" src="/tab_sel_left.png" border="0" type="image"></td>      
              <td align="center" background="/tab_sel_bg.png">
              &nbsp;
                    <a href="javascript:document.forms[0].tabIndex.value='1'-1;document.forms[0].methodCalled.value='gotoTabChange';doSubmit();" tabindex="4" class="menu1itemSel">General</a>
                &nbsp;
                </td>
              <td><input name="" src="/tab_sel_right.png" border="0" type="image"></td>
              <td width="1"></td>   
              <td><input name="" src="/tab_unsel_left.png" border="0" type="image"></td>
              <td align="center" background="/tab_unsel_bg.png">
              &nbsp;
                    <a href="javascript:document.forms[0].tabIndex.value='2'-1;document.forms[0].methodCalled.value='gotoTabChange';doSubmit();" tabindex="4" class="menu1itemUnSel">Options</a>
                &nbsp;</td>
              <td><input name="" src="/tab_unsel_right.png" border="0" type="image"></td>
              <td width="1"></td>
            </tr>
        </tbody>
    </table>
    ...
</b></b>

我的casper.js文件如下所示:

...
    casper.then(function() {
        test.assertTextExists("DB", "Login into DB 2");
        this.click('a#changeThis1_link');
    });

    casper.then(function() {
        test.assertTextExists("Options", "DB Options");
        this.click('a#menu1itemUnSel[tabindex="4"]');
    });

    casper.then(function() {
        test.assertTextExists("Change", "DB -Change -Step 1/2");
        this.fill('form[name="dbActionForm"]', {
          'generalParams.poolSize': '1',
        }, false);
        this.click('input[name="Apply"]');
    });
...

我无法弄清楚这条线应该是什么样的:

this.click( '一个#menu1itemUnSel [tabindex属性= “4”]');

因为这不起作用!!!

执行打印输出:

modifying DB pool size:
Test file: /target.js
# DB modify
PASS DB has the correct title
PASS login form is found
PASS Login into DB
PASS Login into DB 1
PASS Login into DB 2
PASS DB Options
FAIL Cannot dispatch mousedown event on nonexistent selector: a#menu1itemUnSel[tabindex="4"]
#    type: uncaughtError
#    error: Cannot dispatch mousedown event on nonexistent selector: a#menu1itemUnSel[tabindex="4"]
#           CasperError: Cannot dispatch mousedown event on nonexistent selector: a#menu1itemUnSel[tabindex="4"]
#               at mouseEvent (/casper.js:1323)
#               at click (/casper.js:428)
#               at /target.js:34
#               at runStep (/casper.js:1523)
#               at checkStep (/casper.js:368)
#    stack: not provided

任何线索我的错误是什么,我如何克服这个问题?

感谢您的时间

一些更新和更多信息:

在Fanch信息之后,我将casperjs更改为:

...     casper.then(function(){         test.assertTextExists(“DB”,“登录到DB 2”);         this.click( '一个#changeThis1_link');     });

casper.then(function() {
    test.assertTextExists("Options", "DB Options");
    this.click('a.menu1itemUnSel[tabindex="4"]');
});

casper.then(function() {
    test.assertTextExists("Change", "DB -Change -Step 1/2");
    this.fill('form[name="dbActionForm"]', {
      'generalParams.poolSize': '1',
    }, false);
    this.click('input[name="Apply"]');
});

...

这个a.menu1itemUnSel [tabindex =“4”]解决了我关于更改标签的错误,但我仍然有阅读/更改字段generalParams.poolSize的问题。

我甚至添加了waitForSelector / waitForText,但仍然得到错误:填写表单时遇到错误:找不到表单

见casperjs:

...
    casper.then(function() {
        test.assertTextExists("Options", "DB Options");
        this.click('a.menu1itemUnSel[tabindex="4"]');
    });

    casper.waitForSelector("a#dve_menu_datarepositories", function() {
         this.echo("1.Loading form");
    });

    casper.waitForText("50", function() {   //the field that i want to change has text '50'
         this.echo("2.Loading form");
    });

    casper.then(function() {
        test.assertTextExists("Change", "DB -Change -Step 1/2");
        this.fill('form[name="dbActionForm"]', {
          'generalParams.poolSize': '1',
        }, false);
        this.click('input[name="Apply"]');
    });
...

再次感谢

抱歉,我离开了一段时间,这里是页面的一些html部分:

<a name="topofpage">
    <form autocomplete="off" enctype="application/x-www-form-urlencoded" action="/db.do" method="post" name="dbActionForm">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tbody>
                <tr>
                    <td>
                        <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
                            <tbody>
                                <tr height="100%">
                                    <td valign="top" height="100%"></td>
                                    <td valign="top" height="100%" background="/menu3_sel_right1.png"></td>
                                    <td class="commonfont" width="100%" valign="top" background="/menu3_sel_right1.png" align="left">
                                        <b>
                                            <b>
                                                <table class="commonfont" border="0" cellspacing="0" cellpadding="0" onkeypress="return onWizardPageKeyPress(event);">
                                                    <tbody>
                                                        <tr id="generalParams.poolSize_TR" class="formpagefieldname" style="display:table-row;">
                                                            <td>
                                                                <a id="generalParams.poolSize_changeA" style="visibility:hidden;" title="Undo Change" onclick="revertSingleChange('generalParams.poolSize', false); dependantsRunOnLoad(document.body); return false;" href="javascript:;"></a>
                                                            </td>
                                                            <td>
                                                                <input type="text" onblur="if (!this.disabled) {onChangeProperty(this, false);} " onpropertychanged="if (!this.disabled) {onChangeProperty(this, false);} " onkeyup="if (!this.disabled) { onChangeProperty(this, false); }" tabindex="2" title="10" size="30" value="50" name="generalParams.poolSize"></input>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </b>
                                        </b>

我仍然有填写表格的问题!

FAIL Errors encountered while filling form: form not found
#    type: uncaughtError
#    error: Errors encountered while filling form: form not found

更多线索?

谢谢你的时间!!!

1 个答案:

答案 0 :(得分:1)

使用此:

this.click('a.menu1itemUnSel[tabindex="4"]');

- &GT; = div, = class