How to add xpath in a click function?
The Click function using a CSS Selector is
const buttonSelector = cssSelector;
{some function}
$(buttonSelector).click();
But, When I use Css Selector, the clicking goes on an endless loop, since adding more attributes doesnt work with CSS Selector. So, I decided to use Xpath and modified the code like follows, but, I am getting a syntax error.
const buttonSelector = document.querySelector('//Xpath');
{some function}
$(buttonSelector).click();
How can I correct this to make it work?
6 Replies
afraid-scarlet•3y ago
https://developer.mozilla.org/en-US/docs/Web/XPath/Introduction_to_using_XPath_in_JavaScript or consider to use it from framework (Puppeteer, Playwright https://playwright.dev/docs/selectors)
metropolitan-bronzeOP•3y ago
I checked these and couldnt find a solution.
afraid-scarlet•3y ago
If you not getting clickable element by
document.evaluate
then try to get element by Puppeteer or PW and call click()
from framework. If both approached does not work review your data flow since well, scrapers able to do clicks, if your clicks does not work you doing it somehow in a wrong waymetropolitan-bronzeOP•3y ago
My click works when I use css selector, but it goes on an infinite loop.
When I use document.evaluate with xpath, its not detecting the next page button.
I am using the apify/webscraper , so, I am not sure how to use puppeteer in it, do I have to code in the puppeteer crawler?
afraid-scarlet•3y ago
yes, I think so: https://apify.com/apify/web-scraper#page-function not provide
page
object in context
so you need either pptr crawler or your own actormetropolitan-bronzeOP•3y ago
Ok, Thanks, I will try that.