使用javascript和casper.js将javascript代码注入on click事件中 [英] Injecting javascript code into an on click event with javascript and casper.js

查看:122
本文介绍了使用javascript和casper.js将javascript代码注入on click事件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在某些javascript在页面上加载了一些动态内容之后,我才尝试使用python(硒/请求和机械装置)来抓取页面后才开始使用casperjs.

I've just started using casperjs after trying to use python (selenium / requests and mechanise) to scrape a page only after some javascript loaded some dynamic content on the page.

由于硒很难做到或很慢,建议我转向Casper js(需要phantomjs).

Since this was very hard to do or very slow with selenium it was suggested I turn to Casper js (which requires phantomjs).

我想知道的一件事(我对javascript很陌生)与javascript onclick事件有关.

One thing I am wondering (I am quite new to javascript) is relating to a javascript onclick event.

默认情况下,我要抓取的页面每页显示十个名称,底部具有显示(5)或显示(100)的选项.

The page I want to scrape by default shows ten names per page, and at the bottom has options to show (5) or show (100).

深入研究此代码并使用firebug对其进行检查后,我想知道是否可以将onclick = loaditems(100)更改为... onclick = loaditems(Load X items),其中X可能为200. (或将所有内容加载到一页上并使其更易于抓取所需的数字.这有可能吗?

After diving into this code and inspecting it with firebug I am wondering if it is possible to change the onclick=loaditems(100) to something like... onclick=loaditems(Load X items), where X could be 200. (or whatever number it needs to be to load all the content on one page and make it easier for scraping. Is this possible?

更新 *审阅者要求提供用于选择每页100个项目的代码....

update * reviewer asked for the code used to select the 100 items per page....

代码(HTML)是.

<a title="Show 100 items per page"
onclick="lconn.profiles.Friending.setItemsPerPage(this,100)" href="javascript:void(0);">100</a> 

Xpath是...

and the Xpath is...

/html/body/div/div[2]/div[3]/div[3]/span/div/div/div/div/div[2]/div/div/form/div??/div[4]/div/ul/li[4]/a

问题 我可以编辑onclick命令并将其值更改为一个较大的数字,但是我不知道如何使用要在每页上显示的较大数量的元素执行该命令,以查看其是否有效.

problem I am able to edit the onclick command and change the value to a higher number, however I do not know how to then execute it with the higher number of elements I want to display per page to see if it works.

推荐答案

我为此任务使用了一个简单的CSS选择器.您可以使用字符串操作来更改onclick属性.在这种情况下,我将"100"替换为num.我还添加了一个clickIt参数来单击更改的链接.

I used a simple CSS selector for this task. You can change the onclick attribute with string operations. In this case I replace "100" with num. I also added a clickIt argument to click the changed link.

casper.changeItemsPerPageLink = function(num, clickIt){
    casper.evaluate(function(num, clickIt){
        num = ""+num;
        var a = document.querySelector('a[title="Show 100 items per page"]');
        a.innerHTML = num;
        a.title = a.title.replace("100", num);
        a.setAttribute("onclick", a.getAttribute("onclick").replace("100", num));
        if (clickIt) {
            a.click();
        }
    }, num, clickIt);
};

请参阅我的测试代码要点.

这篇关于使用javascript和casper.js将javascript代码注入on click事件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆