我使用PhantomJS和选项--web-security = false。我有一个HTML页面,可以在iframe中嵌入页面:
<HTML>
<HEAD>
<TITLE>Test page</TITLE>
<script src="http://code.jquery.com/jquery-2.2.4.js"></script>
</HEAD>
<BODY>
<HR align="left" width="300">
<P>
<b>New frame:</b>
<div id="newframe">
<iframe id="card_pay_2" frameborder="0" allowtransparency="true" scrolling="no" src="https://money.yandex.ru/embed/small.xml?any-card-payment-type=on&uid=410014480752241&button-text=01&button-size=m&button-color=black&targets=JetChange 31036&default-sum=2040&successURL=https://jetchange.com/success" width="auto" height="54">
</iframe>
</div>
</P>
<HR align="left" width="300">
</BODY>
</HTML>
&#13;
iframe加载遵循HTML代码:
<!DOCTYPE html SYSTEM "about:legacy-compat">
<html class="i-ua_js_yes i-ua_css_standart utilityfocus i-ua_svg_yes i- ua_inlinesvg_yes i-ua_placeholder_yes" id="js">
<head>
</head>
<body>
<div class="b-widget-button b-widget-button_black b-widget-button_m">
<form method="POST" target="_blank" action="/quickpay/confirm.xml">
<input name="label" type="hidden" value="">
<input name="receiver" type="hidden" value="410014480752241">
<input name="quickpay-form" type="hidden" value="small">
<input name="referer" type="hidden" value="">
<input name="is-inner-form" type="hidden" value="true">
<input type="hidden" name="targets" value="JetChange 31036">
<input type="hidden" name="sum" value="2040" maxlength="8">
<input type="hidden" name="successURL" value="https://jetchange.com/success">
<input type="hidden" name="paymentType" value="AC">
<span class="b-button" data-content-block="this">
<img class="b-widget-button__icon b-widget-button__icon_type_any- card" src="/i/b-widget-button__icon_type_any-card.png">
<span class="b-button__inner">Перевести</span>
<input type="submit" class="b-button__input">
</span>
</form>
</div>
</body>
</html>
我想点击iframe中的按钮。可能吗 ? 我尝试了以下代码变体。它没有用:
1. page.evaluate(function() {
$('iframe').contents().find("form[target='_blank']").submit(); --> didn't work
$('iframe').contents().find("form[target='_blank']").trigger('submit'); --> didn't work
});
2. page.switchToFrame(0);
page.evaluate(function(){
var frm = document.querySelector('form[target=_blank]');
frm.click(); --> didn't work
frm.submit(); --> didn't work
frm.trigger('submit'); --> didn't work
});
page.switchToMainFrame();
3. page.switchToFrame(0);
page.evaluate(function(){
function click(elm){
var event = document.createEvent('MouseEvent');
event.initMouseEvent('click',
true, true,
window, 0,
0, 0, 0, 0,
false, false, false, false,
0, null);
elm.dispatchEvent(event); --> didn't work
}
var frm = document.querySelector('form[target=_blank]');
click(frm);
});
page.switchToMainFrame();
4. page.switchToFrame(0);
var point = page.evaluate(function(){
var frm = document.querySelector('form[target=_blank]');
var rect = frm.getBoundingClientRect();
return {
x: rect.left + (rect.width/2),
y: rect.top + (rect.height/2)
};
});
page.switchToMainFrame();
console.log('X: ' + point.x);
console.log('Y: ' + point.y);
page.sendEvent('click', point.x, point.y); --> didn't work