CheerioJS get specific <li> where header text 'What I Want' - node.js

I'm trying to get li elements where the header is 'What I want'
This is my Code:
let wants = []
$$('li').each((wantIdx, wantElement) => {
const want= $(relatedArticleElement).text()
wants.push(want)
})
and this is the HTML i'm trying to parse from:
<div class="side-list-panel">
<h4 class="panel-header">What I Want</h4>
<ul class="panel-items-list">
<li>
1
</li>
<li>
2
</li>
<li>
3
</li>
<li>
4
</li>
<li>
5
</li>
</ul>
</div>
<div class="side-list-panel">
<h4 class="panel-header">What I don't want</h4>
<ul class="panel-items-list">
<li>
a
</li>
<li>
b
</li>
<li>
c
</li>
<li>
d
</li>
<li>
e
</li>
</ul>
</div>
this code gets me every single li elements in the page obviously, is there any way i can only get the lis under the 'What I Want' panel-header?

You can get those with:
$('h4:contains("What I Want") + ul li').get().map(li => $(li).text())

You can try JQuery's contains if Cheerio supports it Example $('td:contains("male")')

Related

BeautifulSoup .find() capturing too much text (how do I narrow it down?)

wondering how to target the "Switch" text on the below html:
<div class="product_title">
<a href="/game/pc/into-the-breach" class="hover_none">
<h1>Into the Breach</h1>
</a>
<span class="platform">
<a href="/game/pc">
PC
</a>
</span>
</div>
<div class="product_data">
<ul class="summary_details">
<li class="summary_detail publisher" >
<span class="label">Publisher:</span>
<span class="data">
<a href="/company/subset-games" >
Subset Games
</a>
</span>
</li>
<li class="summary_detail release_data">
<span class="label">Release Date:</span>
<span class="data" >Feb 27, 2018</span>
</li>
<li class="summary_detail product_platforms">
<span class="label">Also On:</span>
<span class="data">
Switch </span>
</li>
</ul>
</div>
so far I am capturing the "Also On:" text as well (with a lot of spaces) with this code:
self.playable_on_systems_label.setText(self.html_soup.find("span", class_='platform').text.strip() + ', ' + self.html_soup.find("li", class_='summary_detail product_platforms').text.strip())
how do I capture (in this case) only the "Switch" text?
FYI - for the first half of the statement (capturing the "PC") text works fine just not the "also on" text
Thanks in advance,
Your query is getting the entire span element with class="summary_detail product_platforms", which is going to include all the text starting from "Also On:" until "Switch." Try something like .find('a', href=re.compile("^.+switch.+$")) or alternately (using CSS) .select("a[href*=switch]") (solution from here)
you can use BeautifulSoup select() function to navigate the the "Switch" text, check this code!!!
rom bs4 import BeautifulSoup
html = '''<div class="product_title">
<a class="hover_none" href="/game/pc/into-the-breach">
<h1>Into the Breach</h1>
</a>
<span class="platform">
<a href="/game/pc">
PC
</a>
</span>
</div>
<div class="product_data">
<ul class="summary_details">
<li class="summary_detail publisher">
<span class="label">Publisher:</span>
<span class="data">
<a href="/company/subset-games">
Subset Games
</a>
</span>
</li>
<li class="summary_detail release_data">
<span class="label">Release Date:</span>
<span class="data">Feb 27, 2018</span>
</li>
<li class="summary_detail product_platforms">
<span class="label">Also On:</span>
<span class="data">
<a class="hover_none" href="/game/switch/into-the-breach">Switch</a> </span>
</li>
</ul>
</div>'''
soup = BeautifulSoup(html, 'html.parser')
text = soup.select('.summary_detail.product_platforms .hover_none')[0].text.strip()
print(text)
Output:
Switch

Thymeleaf nested list not working

I'm trying to get the nested list working in Thymeleaf. I have tried not nesting the list and the entry.value works. However when I start nesting it, it doesnt show up in the webpage. Students is a Map<String, ArrayList<String>>.
<ul th:each="entry : ${students}">
<li th:text="${entry.key}">
<ul>
<li th:text="${entry.value[0]}"></li>
<li th:text="${entry.value[1]}"></li>
<li th:text="${entry.value[2]}"></li>
</ul>
</li>
</ul>
Currently it looks like this.
th:text attributes replace all any child html elements with the contents of the th:text expression. You have to move the th:text into it's own tag, something like this:
<ul th:each="entry: ${students}">
<li>
<span th:text="${entry.key}" />
<ul>
<li th:text="${entry.value[0]}" />
<li th:text="${entry.value[1]}" />
<li th:text="${entry.value[2]}" />
</ul>
</li>
</ul>

How to add menu link wrapper in drupal 7?

I have a menu, one menu item looking like this:
<li>
LINK TITLE
</li>
And I want this:
<li>
<a href="aaa">
<div class="custom">LINK TITLE</div>
</a>
</li>
Or this:
<li>
<div class="custom">
LINK TITLE
</div>
</li>
How can I do this?
This works:
function theme_menu_link(array $variables) {
$variables['element']['#localized_options']['html'] = true;
$variables['element']['#title'] = '<div class="custom">' . $variables['element']['#original_link']['link_title'] . '</div>';
return theme_menu_link($variables);
}

Unable to extract an html fragment using cheerio

I am using cheerio to perfom some html manipulation on node js server .I have an html string like this
var htmlString =" <ol>
<li>
<p>item1</p>
</li>
<li>
<p>item2</p>
</li>
<li>
<p>item 3</p>
</li>
<li>
<p>item 4</p>
</li>
</ol>
<p>First paragraph</p>
<p>second paragraph</p>
<p>Third paragraph</p>
"
var $ = cheerio.load(htmlString);
var dummy = $("<div></div>")
var item = dummy.append($("*").slice(0,3).clone()).html();
The output returned is
<ol>
<li>
<p>item1</p>
</li>
<li>
<p>item2</p>
</li>
<li>
<p>item 3</p>
</li>
<li>
<p>item 4</p>
</li>
</ol>
<li>item1</li>
<p>item1</p>
The output that I expect is the ordered list followed byparagraph1 followed by paragraph2
Am I doing something wrong or is this a bug in cheerio?
After fiddling with the code for the entire day I finally got the solution. Apparently I was loading the html fragment incorrectly. This worked for me
var $ = cheerio.load();
var dummy = $("<div></div>")
var item = dummy.append($(htmlString).slice(0,3)).html();

How do I select a JS tab in watir-webdriver?

I have a webpage which has tab list, the HTML looks like this for this piece:
<div id="content">
<div class="col span-6">
<div class="section first no-border">
<h2>New Search</h2>
<ul class="tabs clear">
<li id="simple-li" class="current">
<a onclick="switch_search_type('SimpleSearch');; return false;" href="#">Simple</a>
</li>
<li id="structured-li">
<a onclick="redirect_to_search('/search/structured_searches/new'); return false;" href="#">Wizard</a>
</li>
<li id="advanced-li" class="">
</li>
<li id="custom-li" class="">
<a onclick="switch_search_type('ComplexQuerySearch');; return false;" href="#">Custom</a>
</li>
</ul>
<div class="tabbed-panel">
I want to select the "Custom" item in this tab list. I tried multiple things but have failed, some of the things I tried:
browser.li(:id, "custom-li").click
browser.select_list(:id, "custom-li").set("Custom")
browser.link(:xpath, "id('custom-li')/x:a").click
browser.select_list(:id => 'custom-li').select "Custom"
I am new to watir-webdriver. Any feedback and help is greatly appreciated.
Try this:
browser.a(:text => "Custom").click

Resources