简体   繁体   English

在动态元素内部剪切文本,没有id,类,属性等

[英]Scrape text inside dynamic element with no id, class, attributes, etc

The only thing I've got going for me is that the preceding <td> will always have the same (and unique to the document) contents: 我唯一能做的就是前面的<td>总是具有相同的(并且是文档的唯一的)内容:

<td>  
    <label>unique text<label>  
</td>  
<td>dynamic text</td>

I can easily grab it with jQuery in the browser console (the page has jQuery loaded): 我可以在浏览器控制台中轻松地使用jQuery抓取它(页面加载了jQuery):

$("label:contains('unique text')").parent().next().text();

I've been at this for a while and have tried everything I can think of. 我已经在这一段时间了,并尝试了我能想到的一切。

My most recent attempt was to use casperjs' evaluate and: 我最近的尝试是使用casperjs的评估和:

casper.thenEvaluate(function addID() {  
    $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
});  
casper.then(function getText() {  
    var target = this.getHTML('td#uniqueID');  
    this.echo(target);  
});

Which gives me: 这给了我:

CasperError: No element matching selector found: td#uniqueID CasperError:找不到元素匹配选择器:td#uniqueID

Why is my casper.thenEvaluate function not creating the td#uniqueID that I'm looking for? 为什么我的casper.thenEvaluate函数没有创建我正在寻找的td#uniqueID

If I do it like this post's answer : 如果我喜欢这篇文章的回答

casper.then(function getText() {  
    this.evaluate(function addID() {  
        $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
    });
    var target = this.thenEvaluate(function returnText() {  
        return $('#uniqueID').text();
    });
    this.echo(target);
});

I get an [Object Casper] which seems to be exactly what it sounds like. 我得到了一个[Object Casper] ,它看起来就像听起来一样。 It's filled with waitForContent , scrollTo , etc... 它充满了waitForContentscrollTo等......

note: The above code block was incorrect (as was pointed out in this answer by Artjom B.) and was changed to this: 注意:上面的代码块不正确(正如 Artjom B的 回答中所指出的那样 并且改为:

casper.then(function getText() {  
    this.evaluate(function addID() {  
        $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
    });
    var target = this.fetchText('#uniqueID');
    this.echo(target);
});  

The problem still persisted. 问题仍然存在。 See my answer below for the resolution. 请参阅下面的答案以获得解决方案。

If you already tried it like in the linked answer, why not just copy it? 如果您已经在链接的答案中尝试过它,为什么不复制它? Your error is that you use thenEvaluate inside the then block. 您的错误是您在then块中使用thenEvaluate CasperJS works in steps and you scheduled a step where it is not necessary. CasperJS分步工作,您安排了一个不必要的步骤。 This creates another step which is executed later. 这创建了另一个稍后执行的步骤。

Change thenEvaluate to evaluate and it should work fine. 更改thenEvaluateevaluate ,它应该工作的罚款。 While you're at it, you could combine the two: 当你在它时,你可以将两者结合起来:

casper.then(function getText() {  
    var target = this.evaluate(function addID() {  
        $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
        return $('#uniqueID').text();
    });
    this.echo(target);
});

or even 甚至

casper.then(function getText() {  
    this.evaluate(function addID() {  
        $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
    });
    var target = this.fetchText(#uniqueID);
    this.echo(target);
});

I finally got it resolved. 我终于解决了。 When I visit the page in a browser, I click a link and javascript calls a modal via ajax POST with the information I'm grabbing. 当我在浏览器中访问该页面时,我点击一个链接,javascript通过ajax POST调用一个模态,其中包含我正在抓取的信息。 I assumed that in casperjs, it would work the same way when I use: 我认为在casperjs中,当我使用时,它会以相同的方式工作:

casper.thenOpen('http://www.website.net/details.php', {
    method: 'post',
    data:   {
        'id': 'foo',
        'key':  'bar',
    }
});

It looks like casper renders this slightly differently than I had anticipated. 看起来casper的呈现方式与我预期的略有不同。 Even though the main page had jQuery loaded, the modal did not. 即使主页面加载了jQuery,模态也没有。 So I needed: 所以我需要:

clientScripts: ["jquery-2.1.1.min.js"]

Inside my: 我的内心:

var casper = require('casper').create({

});

(casperjs docs on jQuery) (关于jQuery的casperjs文档)

Here is the full script for reference: 以下是完整的脚本供参考:

var url = "http://www.website.net/search.php?key=foobarfoobarfoobarfoobar";

var casper = require('casper').create({
    clientScripts: ["jquery-2.1.1.min.js"]
});

casper.start(url, function() {
    //some other unrelated stuff is going to go here
});

casper.thenOpen('http://www.website.net/details.php', {
    method: 'post',
    data:   {
        'id': 'foo',
        'key':  'bar',
    }
});

casper.then(function getText() {  
    this.evaluate(function addID() {  
        $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
    });
    var target = this.fetchText('#uniqueID');
    this.echo(target);
});  

casper.run();

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

相关问题 如何定位没有ID或Class属性的iframe中的元素? - How to target an element inside an iframe that has no Id or Class attributes? 选择没有特定id,class,xpath等的元素 - Selecting an element with no particular id, class, xpath, etc 如何操作 json 数组中的每个项目转到特定的元素类、href、id、text.. 等? - How to manipulate each item in json array to go to a specific element class, href, id, text..etc? 从没有类或ID的元素中抓取链接-Casperjs - Scrape link from element that has no class or id - casperjs 删除没有ID或类的HTML表文本-JavaScript或jQuery - Scrape HTML table text without ID or Class - JavaScript or jQuery 通过ID和类获取元素内部的元素-JavaScript - Get element inside element by ID and class - JavaScript 通过 class 和 ID - JavaScript 获取元素内部元素 - Get element inside element by class and ID - JavaScript jQuery clone form增加name,class,id,data-id ..etc的所有属性 - jQuery clone form increment all attributes of name,class,id,data-id ..etc 从特定标签中提取文本并允许 id、class 等 - Extract text from certain tag and allow id, class etc 如何通过鼠标单击从 chrome 扩展中获取当前选项卡的元素属性(文本、ID、类等..)? - How may I get the element attributes (text, id, class and so on..) of the current tab, out of a mouse click, from a chrome extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM