seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
4.46k stars 909 forks source link

Best practice open new tab (multiple tab) #2796

Closed adarmawan117 closed 1 month ago

adarmawan117 commented 1 month ago

Is there any best way to open multiple tab.? For example: I have a page that contain links. And then i want to extract all link, and open it in new tab, without close first page.

This is my code

elements = self.sb.find_elements('//a[contains(text(), "members")]')
for elem in elements:
    group_link = elem.find_element('xpath', './/parent::div//parent::div//parent::span//parent::div//parent::div/div[1]/a').get_attribute('href')
    self.sb.driver.uc_open_with_tab(group_link) # here i want to open the new tab

Thanks

mdmintz commented 1 month ago

For UC Mode, don't use multiple tabs. That'll either get you detected, or break tab order. Instead, use multiple drivers. Example:

from seleniumbase import SB

with SB(uc=True, test=True) as sb:
    url = "seleniumbase.io/demo_page"
    sb.driver.uc_open_with_reconnect(url)
    links = sb.get_unique_links()
    for link in links:
        driver2 = sb.get_new_driver(undetectable=True)
        driver2.uc_open_with_reconnect(link)
        print(driver2.title)
        sb.quit_extra_driver()