Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open content in a new tab using Python + Selenium

I am using Python + Selenium.

Browsing this page

https://rovo.co/explore/activities

you will see many activities.

Using Python + Selenium, how can I open each activity in a new tab, and switch to the new tab?

I tried the following code, but it doesn't open in a new tab.

link = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(driver.find_elements(By.CLASS_NAME, "css-vurnku")[0]))

actions = ActionChains(driver)
actions.key_down(Keys.CONTROL)
actions.click(on_element=link)
actions.perform()

Any help is highly appreciated.

like image 405
J K Avatar asked Jun 12 '26 09:06

J K


1 Answers

Here's a SeleniumBase script that goes to that site and opens up the first 20 activities in new tabs.

pip install seleniumbase, and run with python:

from seleniumbase import SB

with SB() as sb:  # By default, browser="chrome" if not set.
    sb.open("https://rovo.co/explore/activities")
    for i in range(20):
        sb.open_new_tab(switch_to=True)
        sb.open("https://rovo.co/explore/activities")
        sb.wait_for_element("div.css-uq1pv2")
        sb.find_elements("div.css-uq1pv2")[i].click()
        sb.sleep(0.25)
    breakpoint()
like image 199
Michael Mintz Avatar answered Jun 13 '26 22:06

Michael Mintz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!