can you help to fix that problem
How can i know and proccess this if the xpath doenst exists
Like... if doenst exista do this, if exists do that
Thank you everyone.
const puppeteer = require('puppeteer');
(async () => {
// set some options (set headless to false so we can see
// this automated browsing experience)
let launchOptions = { headless: false, args: ['--start-maximized'] };
const browser = await puppeteer.launch(launchOptions);
const page = await browser.newPage();
// set viewport and user agent (just in case for nice viewing)
await page.setViewport({width: 1366, height: 768});
await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36');
// go to the target web
await page.goto('https://www.lamudi.co.id/newdevelopments/');
// wait for element defined by XPath appear in page
await page.waitForXPath("(//span[@class='CountTitle-number'])[1]");
// evaluate XPath expression of the target selector (it return array of ElementHandle)
let elHandle = await page.$x("(//span[@class='CountTitle-number'])[1]");
// prepare to get the textContent of the selector above (use page.evaluate)
let lamudiNewPropertyCount = await page.evaluate(el => el.textContent, elHandle[0]);
console.log('Total Property Number is:', lamudiNewPropertyCount);
// close the browser
await browser.close();
})();
Like... if doenst exista do this, if exists do that...
CodePudding user response:
To check if an element exists in the DOM (Document Object Model) using an XPath expression in JavaScript, you can use the evaluate
method of the document
object. This method returns an iterator that you can use to iterate over the elements that match the XPath expression. The iterator will be empty if the XPath expression does not match any elements.
Here's an example of how you can check if an element exists using an XPath expression and the evaluate
method:
function elementExists(xpath) {
// Use the evaluate method to get an iterator over the elements that match the XPath expression
var iterator = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);
// Check if the iterator has any elements
return iterator.iterateNext() != null;
}
// Example usage
if (elementExists("//div[@class='my-class']")) {
// The element exists, do something
} else {
// The element does not exist, do something else
}
Alternatively, you can use the querySelector
or querySelectorAll
document
object methods to search for elements using a CSS selector. If the selector does not match any elements, these methods will return a null
or an empty node list.
Here's an example of how you can check if an element exists using a CSS selector and the querySelector
method:
function elementExists(selector) {
// Use the querySelector method to get the first element that matches the selector
var element = document.querySelector(selector);
// Check if the element exists
return element != null;
}
// Example usage
if (elementExists(".my-class")) {
// The element exists, do something
} else {
// The element does not exist, do something else
}
CodePudding user response:
could you try something like below.
----
----
let elHandle = await page.$x("(//span[@class='CountTitle-number'])[1]");
if(typeof elHandle === 'undefined') {
console.log("it is not defined yet, do something");
} else if (elHandle.length > 0) {
console.log("you have a greater than zero length array, do something");
}
----
----
hope it might help.