I'm trying to trigger click event but in Ubuntu server it is not working.
....
await page.evaluate(() => {
document.querySelector(".phone").firstElementChild.click();
});
.....
I'm trying to trigger click event but in Ubuntu server it is not working.
....
await page.evaluate(() => {
document.querySelector(".phone").firstElementChild.click();
});
.....
Evaluation fails if an element is not present in the DOM (Document Object Model). TypeError: Cannot read properties of null (reading '...')
is the general error in such cases, I can recommend this StackOverflow question/answer on this topic.
(1) As your script works only locally you need to find the reason what's the difference between the scraped page opened locally vs opened on the server:
.phone
class is not rendered to the DOM?For this purpose, you can:
console.log(await page.content())
)await page.screenshot({ path: __dirname + '/screen.png' })
const screenBase64 = fs.readFileSync(__dirname + '/screen.png', 'base64')
console.log('data:image/png;base64, ' + screenBase64)
You need to find the answer for this yourself and the solution should address this.
(2) If your script would throw the same error even locally it would tell that you try to evaluate .phone
too early before it would be rendered to DOM. This could be handled with awaiting the element:
await page.waitForSelector('.phone')
await page.evaluate(() => {
document.querySelector('.phone').firstElementChild.click()
})