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.45k stars 908 forks source link

Unable to use uc_open_with_tab with Driver manager #2862

Closed davidAlgis closed 1 week ago

davidAlgis commented 1 week ago

I'm trying to open a new tab in uc mode with Driver. Here is my code:

from seleniumbase import Driver

driver = Driver(browser="firefox",
                     uc=True, headless=False)
driver.uc_open_with_tab("https://seleniumbase.io/")

After it has launched the browser windows, it throws the following error:

Traceback (most recent call last):                                                                                                                                                                                                                                          
  File "\a.py", line 5, in <module>                                                                                                                                                                                                                     
    driver.uc_open_with_tab("https://seleniumbase.io/")                                                                                                                                                                                                                     
AttributeError: 'WebDriver' object has no attribute 'uc_open_with_tab

If I use get instead of uc_open_with_tab it works perfectly.

mdmintz commented 1 week ago

UC Mode (uc=True) is for Chrome only, although it can also work on some of the other Chromium browsers (eg. Brave, Opera) by setting the binary_location.

Firefox, Safari, and Edge won't work with UC Mode (you set the browser to firefox). Therefore the UC Mode specific methods (such as uc_open_*) won't exist.

davidAlgis commented 1 week ago

Thanks for your answer ! That's interesting, because at first I was using the "classical" selenium module, but it wasn't working for some reason and I switch to seleniumbase to try if it works, I was completely sure it works because of the UC mode, but as you mention it I just notice that it doesn't change anything. Could you explain to me what is the difference then between selenium and seleniumbase if the uc mode is disabled ? Moreover, have you an idea how to open a new tab without uc mode but with Driver ?

mdmintz commented 1 week ago

The SeleniumBase driver has the original driver methods, plus new ones.

Here's an example of a script that opens a new tab with the Driver() manager:

from seleniumbase import Driver

driver = Driver()
try:
    driver.open("data:text/html,<h1>Page A</h1>")
    driver.assert_text("Page A")
    driver.switch_to.new_window("tab")
    driver.open("data:text/html,<h1>Page B</h1>")
    driver.assert_text("Page B")
    driver.switch_to.window(driver.window_handles[0])
    driver.assert_text("Page A")
    driver.switch_to.window(driver.window_handles[1])
    driver.assert_text("Page B")
finally:
    driver.quit()

And since that script runs really fast, here's the same script with waits:

from seleniumbase import Driver

driver = Driver()
try:
    driver.sleep(1)
    driver.open("data:text/html,<h1>Page A</h1>")
    driver.assert_text("Page A")
    driver.sleep(1)
    driver.switch_to.new_window("tab")
    driver.sleep(1)
    driver.open("data:text/html,<h1>Page B</h1>")
    driver.assert_text("Page B")
    driver.sleep(1)
    driver.switch_to.window(driver.window_handles[0])
    driver.assert_text("Page A")
    driver.sleep(1)
    driver.switch_to.window(driver.window_handles[1])
    driver.assert_text("Page B")
    driver.sleep(1)
finally:
    driver.quit()
davidAlgis commented 1 week ago

It works like a charm. Thank you a lot !