如何使用PhantomJS提交表格?

时间:2022-11-24 19:07:04

I'm getting familiar with PhantomJS. But I can't get one thing. I have a page with a simple form:

我熟悉PhantomJS。但我不能得到一件事。我有一个简单形式的页面:

<FORM action="save.php" enctype="multipart/form-data" method="GET" onSubmit="return doSubmit();">
    <INPUT name="test_data" type="text">
    <INPUT name="Submit" type="submit" value="Submit">
</FORM>

and a save.php just writes down the test_data value

并且save.php只记下test_data值

so I'm doing this:

所以我这样做:

page.evaluate(function() {
    document.forms[0].test_data.value="555";
    doSubmit();
});

When rendering the page I see that text field is 555, but form isn't submitting and save.php didn't write down a test_data value. So doSubmit() is not executing, is it? doSubmit() is a simple validation step and a submit is supposed to load the next page.

渲染页面时,我看到文本字段是555,但表单没有提交,save.php没有记下test_data值。所以doSubmit()没有执行,是吗? doSubmit()是一个简单的验证步骤,提交应该加载下一页。

So the question is: how can I execute a javascript code on the page, using PhantomJS?

所以问题是:如何使用PhantomJS在页面上执行javascript代码?

1 个解决方案

#1


9  

It seems that you want to submit the form. You can achieve that in different ways, like

您似乎想提交表单。您可以通过不同方式实现这一目标,例如

  • clicking the submit button
  • 单击“提交”按钮

  • submit the form in the page context:

    在页面上下文中提交表单:

    page.evaluate(function() {
        document.forms[0].submit();
    });
    
  • or focus on the form text field and send an enter keypress with sendEvent().

    或者专注于表单文本字段并使用sendEvent()发送enter键。

After that you will have to wait until the next page is loaded. This is best done by registering page.onLoadFinished (which then contains your remaining script) right before submitting the form.

之后,您将不得不等到下一页加载。最好通过在提交表单之前注册page.onLoadFinished(然后包含剩余的脚本)来完成。

page.open(url, function(){
    page.onLoadFinished = function(){
        page.render("nextPage.png");
        phantom.exit();
    };
    page.evaluate(function() {
        document.forms[0].test_data.value="555";
        document.forms[0].submit();
    });
});

or you can simply wait:

或者你可以等一下:

page.open(url, function(){
    page.evaluate(function() {
        document.forms[0].test_data.value="555";
        document.forms[0].submit();
    });
    setTimeout(function(){
        page.render("nextPage.png");
        phantom.exit();
    }, 5000); // 5 seconds
});

#1


9  

It seems that you want to submit the form. You can achieve that in different ways, like

您似乎想提交表单。您可以通过不同方式实现这一目标,例如

  • clicking the submit button
  • 单击“提交”按钮

  • submit the form in the page context:

    在页面上下文中提交表单:

    page.evaluate(function() {
        document.forms[0].submit();
    });
    
  • or focus on the form text field and send an enter keypress with sendEvent().

    或者专注于表单文本字段并使用sendEvent()发送enter键。

After that you will have to wait until the next page is loaded. This is best done by registering page.onLoadFinished (which then contains your remaining script) right before submitting the form.

之后,您将不得不等到下一页加载。最好通过在提交表单之前注册page.onLoadFinished(然后包含剩余的脚本)来完成。

page.open(url, function(){
    page.onLoadFinished = function(){
        page.render("nextPage.png");
        phantom.exit();
    };
    page.evaluate(function() {
        document.forms[0].test_data.value="555";
        document.forms[0].submit();
    });
});

or you can simply wait:

或者你可以等一下:

page.open(url, function(){
    page.evaluate(function() {
        document.forms[0].test_data.value="555";
        document.forms[0].submit();
    });
    setTimeout(function(){
        page.render("nextPage.png");
        phantom.exit();
    }, 5000); // 5 seconds
});