0

I need to collect all the links from the page, open each one (alternately) in a new tab, collect data from the page and close the tab.

in selenium, I would do something similar to this:

elems = driver.find_elements_by_xpath("//a[@href]")
main_window = browser.current_window_handle
for elem in elems:
    ActionChains(browser).key_down(Keys.COMMAND).click(elem.get_attribute("href")).key_up(Keys.COMMAND).perform()
    browser.switch_to.window(browser.window_handles[1])
    ... collect data ...
    browser.close()
    browser.switch_to.window(main_window)

How to implement this functionality in playwright?

hardkoded
  • 18,915
  • 3
  • 52
  • 64
r_motion
  • 23
  • 6
  • Does this answer your question? [How do I switch to new tab or window in playwright-java?](https://stackoverflow.com/questions/66638076/how-do-i-switch-to-new-tab-or-window-in-playwright-java) – Christian Baumann Dec 27 '21 at 08:00

1 Answers1

0

You can pass modifiers to the click function:

page.click('a', { modifiers: [ isMac ? 'Meta' : 'Control'] }),

Note: isMac is a made up variable, you should determine if you have to use Meta (cmd key on mac) or Control on Linux/Windows

hardkoded
  • 18,915
  • 3
  • 52
  • 64