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
5.36k stars 980 forks source link

Tabs do not work correctly #2233

Closed GOOGLI4CH closed 1 year ago

GOOGLI4CH commented 1 year ago

Hi, I'm having trouble working with tabs when parsing.

My trouble: I use seleniumbase in my code with the help of context manager SB. After creating a new tab and loading it, the focus goes to the previous tab (I use the _open_newwindow() method with the default parameter to_switch=True), the focus is expected on the current/just opened tab. Also when using manual switching to a window using the _switch_towindow() method with parameter 0, it switches to the current tab, but is expected to the previous tab (first tab). I would also like to know how to close tabs without using such calls: self.sb.driver.close(), as it violates the very principle of seleniumbase.

My use of seleniumbase

with SB(headless=False, undetectable=True, proxy=self.__proxy, page_load_strategy='eager') as self.__driver:
    self.__driver.open(self.__url)

    # Open new window
    self.__driver.open_new_window()
    self.__driver.open(url)

    # Switch to the new tab
    self.__driver.switch_to_window(0)

    # Close tab
    self.__driver.driver.close()

    # Switch to the start tab
    self.__driver.switch_to_window(-1)
mdmintz commented 1 year ago

Here's a basic example of tab-switching:

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.open("data:text/html,<h1>Page A</h1>")
    sb.assert_text("Page A")
    sb.open_new_window()
    sb.open("data:text/html,<h1>Page B</h1>")
    sb.assert_text("Page B")
    sb.switch_to_window(0)
    sb.assert_text("Page A")
    sb.assert_text_not_visible("Page B")
    sb.switch_to_window(1)
    sb.assert_text("Page B")
    sb.assert_text_not_visible("Page A")

Here's one with your example after patching it up:

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.driver.uc_open("data:text/html,<h1>Page A</h1>")

    # Open new window
    sb.open_new_window()
    sb.driver.uc_open("data:text/html,<h1>Page B</h1>")

    # Switch to the new tab
    sb.switch_to_window(0)
    sb.assert_text("Page A")

    # Close tab
    sb.driver.close()

    # Switch to the start tab
    sb.switch_to_window(-1)
    sb.assert_text("Page B")

Now with undetected mode, if you're trying to open a URL that has bot detection on it, it will open the URL in a NEW TAB automatically, and it will ALSO close the current tab you're on. This may change the tab-numbering. That's why there's a special driver.uc_open(url) method, which will open the URL in the current tab (but it also means you might get detected). This is mentioned in this video: https://www.youtube.com/watch?v=5dMFI3e85ig (around the 24-minute mark).

driver.close() is the correct way to close a tab. (Or sb.driver.close() for that format.)

I've tested both scripts above on a Mac without issue.

GOOGLI4CH commented 1 year ago

Thanks