How can I extract an attribute from multiple elements in Playwright? - playwright-dotnet

I'm trying to extract some attributes from multiple elements using playwright.
( this is F# but you should be able to figure out what is wrong )
let! pagelinkRows = page.EvalOnSelectorAllAsync("tr[title]","(tr) => tr.getAttribute("title")") |> Async.AwaitTask
I get the error: tr.getAttribute is not a function. I have also tried tr.attr .
What is the simplest way to get the attributes from a collection of elements
<tr title="xxx1"></tr>
<tr title="xxx2"></tr>
<tr title="xxx3"></tr>
<tr title="xxx4"></tr>

Related

Finding the contents under div with specific id patterns using MOJO::DOM

I need to parse some HTML codes. The patterns of the tag ID are:
<tr id="date">.....</tr>
<tr id="band01"><td>field1</td><td>field2</td></tr>
<tr id="band02">...contents...</tr>
.....
<tr id="(others">.....
I'm using PERL Mojo::DOM parser, and want to extract all the actual ids with names starting with "band" following by a number, as well as its contents.
How could I achieve this?
The E[foo^="bar"] selector matches any element with a "foo" attribute starting with "bar". Thus you can use:
my $dom = Mojo::DOM->new($html);
my $rows = $dom->find('tr[id^="band"]');
$rows would be a Mojo::Collection of Mojo::DOM objects representing each matching element and its respective contents. For example, to get the list of matched IDs:
my #ids = $rows->map(attr => 'id')->each;
Or with more standard Perl:
my #ids = map { $_->{id} } #$rows;

Web2py orderby, limitby

My database layer (MongoDB):
db.define_table('news',
Field('title', label='Title'),
Field('link', label='Link'),
Field('date', label='Date'),
Field('summary',label='Summary'))
My controller:
def news():
if len(request.args): page=int(request.args[0])
else: page=0
items_per_page=5
limitby=(page*items_per_page,(page+1)*items_per_page+1)
orderby="~date"
qset = db(db['news'])
grid = qset.select(orderby=orderby, limitby=limitby)
return dict(grid=grid,page=page,items_per_page=items_per_page)
My view:
<table class="news-stories">
<th>Date</th>
<th>Title</th>
<th>Summary</th>
{{for i, row in enumerate(grid):}}
{{if i==items_per_page:break}}
<tr>
<td>{{=row.date.strftime("%m/%d/%Y")}}</td>
<td>{{=row.title}}</td>
<td>{{=row.summary}}</td>
</tr>
{{pass}}
</table>
{{if page:}}
previous
{{pass}}
{{if len(grid)>items_per_page:}}
next
{{pass}}
When I set orderby="~date" or orderby="date desc", as in the example above, my dates are not ordered appropriately in desc order; some dates in November come after dates in December (and vice-versa). However, when I set orderby="date", the dates are ordered perfectly in asc order. Am I missing something here?
Dates are inserted as datetime.datetime.strptime(date,"%m/%d/%Y").
It should be:
orderby = ~db.news.date
See details in the documentation.

Loop over the Mongodb objects to get the values in Meteorjs

I am first Time working With Meteorjs and MongoDB. I am working on a Q&A application. where I have a list of objects which i got using Meteor database call. I want to looper over the list and want to split one of its attribute to the length 50 and then want to send a list of these splitted objects to the database.what I have is this
Template.questions.questions = function () {
questions= Meteor.questions.find({topic_id: Session.get("currentTopicId")});
return questions
}
Every question object in the questions list has an attribute question_text. Using loop i want to split this attribute to length of fifty and then want to push that to an empty list and return that list . Like
questions_list=[]
for(i=0;i<questions.length;i++){
question=questions[i].question_text[0:50] // python syntex to get first 50 char of a string
questions_list.push(question
}
return questions_list
and My HTML is like
<tbody>
{{#each questions}}
<tr>
<td class="span2" style="overflow:hidden;width:50px">
{{question_text}}
</td>
</tr>
{{/each}}
</tbody>
suggest me guys how can i acieve this in meteorjs. my problem is when i try to loop over this questions list there are many attributes like Collection, Results, Queries. here i am unable to iterate this list of objects.
In the same way how can i get the error message thrown back by meteorjs
This will give you list of shortened texts of all questions matching a query:
var questions = Questions.find({...}).map(function(question) {
return question.text.substr(0,50);
});
You could use it directly in a helper:
Template.questions.questions = function () {
return Questions.find({...}).map(function(question) {
return question.text.substr(0,50);
});
};
By the way, Questions.find({...}) does not return a list of questions, but a cursor object that you can use to manipulate query data in an effective way (like that map method above). To get raw array, you need to use fetch on that cursor: Questions.find({...}).fetch().
If you're just displaying the data you could create a handlebars helper:
Template.questions.helpers({
first50: function(question) {
return question.text.substr(0,50);
}
});
Then add the helper:
Template.questions.questions = function() {
return Questions.find({...});
};
Then in your template you could do:
<tbody>
{{#each questions}}
<tr>
<td class="span2" style="overflow:hidden;width:50px">
{{first50 question_text}}
</td>
</tr>
{{/each}}
</tbody>

Entity framework in MVC 4 - Include returning false result

I have two tables:
Products:
ProductID, ProductName
Compounds:
CompoundID, ComponentID, Qty
bound as next:
1 Products.ProductID --> many Compounds.CompoundID
1 Products.ProductID --> many Compounds.ComponentID
A Products item can be a compound, a components, or both.
I need that a view returns for each compound its components names and Quatities.
MyController method :
public ActionResult CompoundProduct()
{
var Compounds = db.Compounds.Include(s => s.Products);
return View(Compounds.ToList());
}
MyView code :
#model IEnumerable
<table>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.CompoundID)
</td>
<td>
#Html.DisplayFor(modelItem => item.ComponentID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Products.ProductName)
</td>
</tr>
}
</table>
With this method I get a list of repeated Compound name instead of distinct Component names per Compound as shown in the image below:
any help please ?
When you created the model from the database EF should have created two navigation properties Products and Products1 related to the two foreign keys CompoundID and ComponentID. You are probably using the wrong navigation property that belongs to CompoundID, not the one that belongs to ComponentID. Instead of Products try to use Products1 in the Include...
var Compounds = db.Compounds.Include(s => s.Products1);
...and in the view:
#Html.DisplayFor(modelItem => item.Products1.ProductName)

Using dom to get content from table with no id

I don't know how to get values from tables that don't have an ID.
I would like to use DOM to get the value "03" from
<tr><th scope='row'>Total WUs</th><td>03</td></tr>
but the table doesn't have an ID. It is the first table on the page. I'll give a code printout of the whole table:
<table cellspacing='0' rules='all' border='1' style='border-collapse:collapse;'><tr><th scope='row'>User Name</th><td>laughing_squid</td></tr><tr><th scope='row'>Project Rank</th><td>1,156,036</td></tr><tr><th scope='row'>Team Rank</th><td>2</td></tr><tr><th scope='row'>Total Points</th><td>207</td></tr><tr><th scope='row'>Total WUs</th><td>03</td></tr><tr><th scope='row'>Points Yesterday</th><td>NA</td></tr><tr><th scope='row'>Points Today</th><td>69</td></tr><tr><th scope='row'>PPD Average</th><td>138</td></tr></table>
Check this out, this script uses JQuery which can be downloaded from here:
$('table td').each(function(){
console.log($(this).text());
});
if you need only the value 03 then use following:
console.log($('table td:eq(4)').text());