Reputation: 123
Assume that, I have
countries/eg
countries/us
countries/gr
etc.
I want to take only country code part as a string for each item in details as cc. But I want to do that during the page.evaluate operation. In my code I had undefined for each item in this case. How can I do that? Please help me. Thanks in advance.
getDetails: async (lineBaseSelector) => {
try {
let url = await page.url();
function parseUrl( url ) {
var a = document.createElement('a');
a.href = url;
return a;
}
const details = await page.evaluate((x) => {
const data = {
cc : parseUrl(document.querySelector(`${x}`).getAttribute('href').toLowerCase()).search,
name: document.querySelector(`${x}`).innerText.toLowerCase(),
url: document.querySelector(`${x}`).getAttribute('href'),
last_update: Date.now()
}
return Promise.resolve(data);
}, lineBaseSelector);
return details;
} catch (error) {
console.log(error)
}
}
Upvotes: 1
Views: 37
Reputation: 123
const data = {
cc : document.querySelector(`${x}`).getAttribute('href').toLowerCase().toString().split("/")[1],
name: document.querySelector(`${x}`).innerText.toLowerCase(),
url: document.querySelector(`${x}`).getAttribute('href'),
last_update: Date.now()
}
It is possible with split oepration. In here, index is 1 because of the first part of url has index of 0th.
Upvotes: 1