José Otavio
José Otavio

Reputation: 3

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

Also can be this item

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)
  }
  
 })();

Upvotes: 0

Views: 6788

Answers (2)

José Otavio
José Otavio

Reputation: 3

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();
} 

Upvotes: 0

MC Naveen
MC Naveen

Reputation: 515

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

  1. Right-click on the Element in Browser Devtools.
  2. Select Copy, then Copy Selector

enter image description here

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();

Upvotes: 2

Related Questions