0

I am crawling with CasperJS, but I need to click something to finish the search process of the page at first. But I cannot select the spcific option by its value or its content.

1.Once click the button "All" in the parent frame:

<select name="sort_id" id="sort_id" class="p9"; display: block;" onclick="javascript: window.setTimeout('Hide_Select(&quot;sort_id&quot;,false)',3); showDialog('LIST','SearchList.aspx?ddl_id=sort_id&amp;key=lar~sort_id&amp;EncodingName=',460,360,true);return false;">
        <option value="">All</option>
    </select>

2.The page with jump to the child frame:

<div id="b_div" class="ym-body">
<iframe>
#document
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>
</head>
<body>
  <form method="post" action="SearchList.aspx?ddl_id=sort_id&amp;key=lar%7esort_id&amp;EncodingName=" id="form1">
    <table>
        <tbody>
          <tr>
            <td>
              <select size="2" name="listb_sor" id="listb_sor" onchange="javascript:OptionClick(this,'sort_id');" style="height:300px;width:220px;">
                 <option value="">All</option>
                 <option value="001">OptionA</option>
                 <option value="00001">OptionB</option>

               </select>
               .......
</iframe>

3.I want to select OptionA. If success, the child frame will automatically close. But I cannot sucessfully select OptionA.

Currently my code is:

casper.then(function(){
    this.click('#sort_id.p9');
    this.page.switchToChildFrame(0);

    this.evaluate(function() {
        var sel = document.querySelectorAll('#listb_sor option');
        sel.val('001').onchange();
    });
});

But when I get captured page, it seems the code cannot select OptionA sucessfully.

Jeffy
  • 121
  • 1
  • 2
  • 10

1 Answers1

0

You can got over the iFrame ID if the IFrame got something like that:

casper.then(function() {
  this.click('#sort_id.p9');
});

casper.withFrame("IFrameID", function() {
  casper.then(function() {
    this.evaluate(function() {
      var sel = document.querySelectorAll('#listb_sor option');
      sel.val('001').onchange();
    });
  })
  casper.then(function() {
    // do other stuff in the IFrame
  })

});

casper.then(function() {
  // continue outside the IFrame
});

If the IFrame got no ID you have to go over the index of the Iframe, dependend on the IFrames on the page:

... 
casper.withFrame(1, function() {
  casper.then(function() {
    this.evaluate(function() {
      var sel = document.querySelectorAll('#listb_sor option');
      sel.val('001').onchange();
    });
  })
  casper.then(function() {
    // do other stuff in the IFrame
  })
});
...

For further information look here on the official documentation

dasmelch
  • 532
  • 3
  • 11
  • thx! can you tell me how to get the index of the iframe? The frame has no ID. – Jeffy Mar 07 '17 at 17:29
  • It's the nth-elment of the Page in that case, the iframes. So first iframe is index 0, second Index 1 and so on. If there dynamicly loaded iframes on the site it could be a problem. ;) – dasmelch Mar 07 '17 at 19:25