简体   繁体   English

用CasperJs本身加载页面时加载页面的Javascript

[英]Load The Javascript Of The Page While Loading The Page Itself With CasperJs

I am trying to submit a form. 我正在尝试提交表格。 When I am using firefox the page will be loaded then I open the firebug I can see a input field named "content_html" but when I go to check the source of the page from "view page source" I do not see the input field "content_html" . 当我使用firefox时,页面将被加载,然后打开firebug,我可以看到一个名为“ content_html”的输入字段,但是当我从“查看页面源代码”中查看页面的源代码时,我看不到输入字段“ content_html” I checked further and found out there is a javascript which will be loaded in the browser to show that input field. 我进一步检查,发现有一个javascript将在浏览器中加载以显示该输入字段。 The code is like 代码就像

geteditorinit("http://example.com/pub","data[content_html]",298996,1,750,350,0,0,"content_html");

So I can conclude that "content_html" hidden input field will only be loaded after this javascript code is executed when I visit the page which contains this form. 因此,我可以得出结论,当我访问包含此表单的页面时,仅在执行此javascript代码之后才会加载“ content_html”隐藏的输入字段。 I need to assign some value to this input field to be able to submit the form. 我需要为该输入字段分配一些值才能提交表单。 Since I cannot get the manipulated Javascript HTML ,like the one I get in the browser, with CasperJs I am not able to assign any value to it with CSS selector. 由于我无法像在浏览器中那样获得可操纵的Javascript HTML,因此使用CasperJs无法使用CSS选择器为其分配任何值。 Because the CSS selector does not find this hidden input field. 因为CSS选择器找不到此隐藏的输入字段。 How can I load the page with the javascript it contains so I can get HTML like firebug which shows the input field to me to be able to submit it? 如何加载包含其中的javascript的页面,以便获得类似于firebug的HTML,该HTML会向我显示输入字段以便提交?
After I login, I go to the page which contains the form I want to be submitted like this 登录后,我转到包含要提交的表单的页面

casper.thenOpen(POST_URL, function(){
          //Here I type the code to fill the form
});

PS [ 1 ] If the PhantomJS is a full browser and I don't need to run Javascript then why I cannot fetch this hidden input field with CSS selector? PS [1]如果PhantomJS是完整的浏览器,并且我不需要运行Javascript,那么为什么不能使用CSS选择器获取此隐藏的输入字段呢? 在此处输入图片说明
The above picture is taken from Firebug . 上图是从Firebug拍摄的。 As you see it shows the input field in colorless mode. 如您所见,它以无色模式显示输入字段。 I want to be able to select "content1_html" and set value to it then submit my form. 我希望能够选择“ content1_html”并为其设置值,然后提交我的表单。
PS [ 2 ] I have found that when I load the posting page, it will separately make an ajax request to another page to autosave the content of the "content1_html" . PS [2]我发现,当我加载发布页面时,它将单独向另一个页面发出ajax请求以自动保存“ content1_html”的内容 I need to open the posting page, make a post request for "content_html" to another page, after that click the submit on the page I have already loaded. 我需要打开发布页面,向另一个页面发出“ content_html”的发布请求,然后单击我已经加载的页面上的提交。 Can I make another tab or open another url without losing the data I already have? 我可以制作另一个标签或打开另一个网址,而不会丢失已有的数据吗? because after each refresh the form token would be changed and I will not be able to submit the post successfully. 因为每次刷新后,表单令牌都会更改,而我将无法成功提交该帖子。

There are multiple ways to select that element. 有多种选择该元素的方法。 You can try for example the following CSS selectors and they would work depending on which elements are also on the page: 您可以尝试例如以下CSS选择器,它们将根据页面上的哪些元素而起作用:

  • "input[name = 'data[content1_html]']" based on exact name attribute match 基于确切名称属性匹配的"input[name = 'data[content1_html]']"
  • "input[id ^= 'hdndata[content1_html]']" based on beginning of id attribute 基于id属性开头的"input[id ^= 'hdndata[content1_html]']"

Now, to change the value, you need to use casper.evaluate() , because the DOM can only be accessed through this function and CasperJS doesn't provide any convenience functions for this case. 现在,要更改值,您需要使用casper.evaluate() ,因为只能通过此功能访问DOM,而CasperJS在这种情况下不提供任何便利功能。 It's sandboxed, so you need to explicitly pass the values in. 它是沙盒,因此您需要显式传递值。

casper.evaluate(function(selector, value){
    document.querySelector(selector).value = value;
}, selector, value);

It may be necessary to wait until that input field is present in the page: 可能需要等到页面中出现该输入字段:

casper.waitForSelector(selector, function then(){
    this.evaluate(function(selector, value){
        document.querySelector(selector).value = value;
    }, selector, value);
    // Do something to submit the form
});

If it is enough to add this hidden input to the DOM before submitting the form, here is the code for this: 如果足以在提交表单之前将此隐藏输入添加到DOM,则此代码如下:

casper.evaluate(function(value){
    var parent = document.querySelectorAll(".rteDiv")[1];

    // get dynamic ID
    var id = parent.querySelector("[id ^= 'Buttons1_']").id.replace("Buttons1_rte", "");

    var input = document.createElement("input");
    input.type = "hidden";
    input.id = "hdndata[content1_html]_" + id;
    input.value = value;
    input.name = "data[content1_html]";

    parent.appendChild(input);
}, value);

I assume that the second .rteDiv is the one that you want to use, since the other one is the demo. 我假设第二个.rteDiv是您要使用的那个,因为另一个是演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM