'How do i click in a div class using puppeteer?
I'm trying to click in a button but i only have the class name, i'm trying to use page.click, but never work so i try to add waitForSelector and always gives me time out, my code is very poor i'm just trying to learn more about to create new projects.
this is the item i'm trying to get
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
(async () => {
const browser = await puppeteer.launch({
headless: false,
product : "chrome",
});
const page = await browser.newPage();
await page.setViewport({
width:1920,
height:1080
})
try{
await page.goto('https://br.betano.com/login/')
await delay(1000)
await page.keyboard.type('*******')
await page.keyboard.press('Tab')
await page.keyboard.type('******')
await page.keyboard.press('Enter')
await delay(5000)
await page.goto('https://br.betano.com/casino/live/games/roleta-brasileira/444/tables/103910/')
await delay(2000)
await page.keyboard.press('Escape')
await delay(10000)
await page.waitForSelector('[class="header__more-games"]')
await page.click('[class="header__more-games"]')
}catch(err){
console.error(err)
}
})();
Solution 1:[1]
You can always use the selector to select a particular element and click on it or you can also use Xpath.
Here is how you can get the Selector
- Right-click on the Element in Browser Devtools.
- Select Copy, then Copy Selector
Now, Replace these lines with the below one.
await page.waitForSelector('[class="header__more-games"]')
await page.click('[class="header__more-games"]')
Make sure to replace the Your Selector Here with the actual selector you copied in the above steps.
await page.waitForSelector('Your Selector Here');
const buttonClick = await page.$("Your Selector Here");
await buttonClick.click();
Solution 2:[2]
I figure how to do it by using link in an iframe, in my case the link is always the same, so I just get the link and open in another tap. Like this:
await page2.goto('https://cachedownload-br.p-content.gambling-malta.com/live/?tableId=103910&game=rol&preferedmode=real&language=pt-br&advertiser=ptt/&clienttype=casino')
await page2.bringToFront()
await delay(15000)
const [button] = await page2.$x("//button[contains(., 'Ok')]");
if (button) {
await button.click();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Tyler2P |

