← Back to Discord Forum

Open new url in a new Window

As requirement from a client of mine I need to change the default target of a click action from opening the page in a new tab to opening in a new window.

The href attribute is not available on the elemnt I'm going to click.

A js function is triggered.

the relevant piece of code is:

// Loop through all games, selecting ROULETTE game type and no AUTOROULLETE
  for (const game of await page.locator('[data-table-type="Roulette"]').all()) {
    const gameType = (await game.locator('[class="live-dealer--table--name"]').innerText()).toUpperCase()
    const dealer = (await game.locator('[class="live-dealer--dealer-name"]').innerText()).toUpperCase()
    if (gameType === 'ROULETTE' && dealer !== 'AUTOROULETTE') {
      [casinoGamePage] = await Promise.all([
        game.locator('[class="live-dealer--table__limit-option"]').getByText('$1-$100').click() // Opens a new tab

how to achieve that?

This thread is trying to answer question "How can I change the default target of a click action from opening the page in a new tab to opening in a new window when the href attribute is not available?"

0 replies


Sure, you can use Playwright's page.click() method with the target option set to _blank to force the click action to open in a new window. Here's how you can do it:

import { chromium } from 'playwright';

async function changeClickTarget() {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();

  // Navigate to your desired webpage
  await page.goto('https://ray.run');

  // Find the element you want to click on
  const element = await page.$('#myElement');

  // Perform the click action with target set to _blank
  await element.click({ target: '_blank' });

  // Close the browser
  await browser.close();

changeClickTarget().catch((error) => console.error(error));

In this code, we're launching a Chromium browser, creating a new context and page, and navigating to https://ray.run. We then locate the element you want to click on and perform the click action with { target: '_blank' } as an option. This forces the click to open in a new window.

Remember, if there's no href attribute for an element, it may not be possible for automation tools to determine whether it should open in a new tab or window. In such cases, using the target option with _blank is a common workaround.

Related Discord Threads

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.