Malformed attribute selector Cheerio - node.js

I'm Requesting to a website and getting the html successfully and loading it with cheerio.
The problem is that its erroring
const res = await axios.get(download_url);
const $ = cheerio.load(res.data);
let result = $('div[class=w-full mt-6 sm:mt-8 lg:mt-0 lg:w-1/3]').html()
So what I think is causing the error is the spaces but what would I have to replace them with?
Thanks,

You would either quote the value:
div[class="w-full mt-6 sm:mt-8 lg:mt-0 lg:w-1/3"]
or use . style:
div.w-full.mt-6.sm:mt-8.lg:mt-0.lg:w-1/3
Also chances are you don't need to full class:
div.w-full.mt-6.sm:mt

Related

Playwright test library - parent element for selector

please, how to get a parent element for a text selector by the Playwright E2E library.
Is better to modify the selector (it is string by something like >> //:parent) or evaluate the selector and then call the DOM element?
(The selector content is unknown)
Thank you.
You can call .$ to start searching from the element:
const elem = await page.$(anySelector)
const parent = await elem.$('xpath=..')
Doc: https://playwright.dev/docs/api/class-elementhandle#elementhandleselector
Using the new Locator you can do:
const elementParent = await page.locator(`${childSelector} >> xpath=..`)
https://playwright.dev/docs/api/class-locator
https://playwright.dev/docs/selectors#xpath-selectors

append a value to existing url in reactjs

I have the following url and a parameter :
number:200
"http://localhost:8000/textpath"
I want the path to be like this:
"http://localhost:8000/textpath/200"
How do I do that using Reactjs?I want to use the appended url in fetch method as follows:
fetch("http://localhost:8000/textpath/200")
A simple append did the work.Doesn't have to complicate
fetch("http://localhost:8000/textpath/"+number)
Try using Template literals.
const url = `http://localhost:8000/textpath/${number}`
fetch(url);
where number = 200
Refer here for more information

Can't retrieve elements with xpath from an OpcUa Nodeset

I am trying to query with xpath this XML file (https://github.com/OPCFoundation/UA-Nodeset/blob/master/Robotics/Opc.Ua.Robotics.NodeSet2.xml).
I am trying to get all the elements inside the tag "UANodeSet" but I always get an empty return.
I am working with node js
Here it is my code:
nodes = fs.readFileSync(Nodeset+".xml", "utf-8");
var data = xpath.select("//UANodeSet", nodes)
fs.writeFileSync("./data.xml" , data);
The library that I am using comes from npm and it is called xpath.
The expectation is having all the child elements inside UANodeSet, reality is that I am having an empty return.
Do you know how to solve it?
Sorry, first time working with xpath.
Ok, first problem is done.
Now I am trying to retrieve all the UAObjectType, but it seems like the xml output is really wrong foermatted and I donĀ“t know why.
Here it is the code:
var select = xpath.useNamespaces({"ns" : "http://www.w3.org/2001/XMLSchema-instance" , "ns1" : "http://opcfoundation.org/UA/2011/03/UANodeSet.xsd" , "ns2" : "http://opcfoundation.org/UA/2008/02/Types.xsd", "ns3" : "http://www.siemens.com/OPCUA/2017/SimaticNodeSetExtensions" ,"ns4" : "http://www.w3.org/2001/XMLSchema" });
var data = select('//ns1:UAObjectType' , ns)
fs.writeFileSync("./data.xml" , data);
Does anyone knows how to solve it?
The xml file uses namespaces and that seems to trip up the xpath expression. Try something like this and see if it work:
dom = require('xmldom').DOMParser
var doc = new dom().parseFromString(nodes)
var targets = xpath.select("//*[local-name()='UAObject']", doc)
The other solution is:
var select = xpath.useNamespaces({"ns1" : "http://opcfoundation.org/UA/2011/03/UANodeSet.xsd" });
var data = select('//ns1:UAObjectType' , ns)

pretty url search string instead of question marks

I have been looking at other websites and see when you search, primary something like a search term, location and category you will see a pretty url like:
example.com/black-boots/new-york/shoes
instead of what I have now which is something like:
example.com/search-results/search?=black+boots&city=new+york&category=shoes
In my route I could start with something like:
router.get('/search-results/:search/:city/:category', shopController.getSearchResults);
And in the controller I could use req.params.city and so on to get the values from the url but the part that I can't figure out is a good way to get the text input values into the url using a get request.
Using GET by default gives me the 'ugly' looking url.
Basically the part that needs to go into the form
<form method="GET" action="/search-results/search/city/category">
Comments, plus this code sample for a GET request:
const form = document.getElementById('searchform');
form.addEventListener('submit', evt => {
const who = encodeURIComponent(document.getElementById('who').value);
const where = encodeURIComponent(document.getElementById('where').value);
const what = encodeURIComponent(document.getElementById('what').value);
window.location.href = `/${who}/${where}/${what}`;
}

What's the expected behavior of puppeteer's ElementHandle.getProperty()?

Puppeteer 1.0.0-post. The getProperty() method seems somewhat magical. For example, if your page contains:
link
Then this will return not a relative but an absolute URL:
const propertyHandle = await elementHandle.getProperty('href');
const href = await propertyHandle.jsonValue();
// href is 'https://localhost:8080/foo/bar.html'
On the other hand, if you were to do the more roundabout:
const hrefHandle = await page.evaluateHandle(element => element.getAttribute('href'), elementHandle);
const href = await hrefHandle.jsonValue();
// href is '/foo/bar.html'
As far as I can tell, the puppeteer documentation doesn't mention this behavior of getProperty()?
It gets uglier, for instance if you want to get the style attribute of an element. It looks like puppeteer's getProperty() actually tries to parse the style in some way, which parsing is buggy/incomplete. The only way to get the raw text is with the roundabout call to evaluateHandle(...).
Is this an intended feature and simply a documentation bug? Or is it just, outright, a puppeteer bug?
Thanks.
See HTML - attributes vs properties for difference between HTML attributes and DOM properties.
You can easily see the difference without Puppeteer, too. For example, on this page:
document.getElementById('nav-questions').href
// returns "https://stackoverflow.com/questions"
document.getElementById('nav-questions').getAttribute('href')
// returns "/questions"

Resources