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

Is it possible to add selenum webdriver remote with seleniumbase. #2675

Closed subh-sk closed 7 months ago

subh-sk commented 7 months ago

Is it possible to interact SauceLab testing platform with seleniumbase. I need to use SauceLab platform to run my seleniumbase script with uc True , but i don't know how to do configuration with seleniumbase to achieve this.

from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver import Remote
options = ChromeOptions()
options.browser_version = 'latest'
options.platform_name = 'Windows 11'
sauce_options = {}
sauce_options['username'] = 'username'
sauce_options['accessKey'] = 'accesskey'
sauce_options['build'] = 'selenium-build-0HI0J'
sauce_options['name'] = 'testing'
options.set_capability('sauce:options', sauce_options)

url = "https://ondemand.eu-central-1.saucelabs.com:443/wd/hub"
driver = Remote(command_executor=url, options=options)

with SB(uc=True,headless2=True,chromium_arg="--no-sandbox --disable-dev-shm-usage --disable-gpu") as sb:
        testing_function(sb=sb)

This is the configuration i want to add with seleniumbase with UC True.

mdmintz commented 7 months ago

See the Selenium Grid ReadMe SeleniumBase/seleniumbase/utilities/selenium_grid/ReadMe.md for using SeleniumBase with a Selenium Grid, and then see SeleniumBase/examples/capabilities for examples of configuring it.

(Note that UC Mode is not compatible with the Grid)

strangeh21 commented 4 months ago

Hi, I have a similar usecase in that I want to use SeleniumBase with an antidetect browser such as Multilogin, without using pytest.

In short, I want to use SeleniumBase's advanced driver functions together with an antidetect browser such as Multilogin. I do not use pytest, this is to be used within an existing program.

Based on what I've read on the docs, this does not seem possible? I was unable to see anything in the docs to indicate I can use raw SeleniumBase driver with a remote webdriver.

I'd love to hear if I can use SeleniumBase with my usecase or if I should stick to vanilla Selenium. Thank you in advance. EDIT: #2071

Turns out you've already answered this. Apologies. I was however unable to get it working as intended. If it is not too much to ask I would love some guidance on if it even is possible to do something similar to driver = Driver(browser="remote", servername="127.0.0.1", port="1234")

mdmintz commented 4 months ago

There are lots of examples that use SeleniumBase UC Mode without pytest: SeleniumBase/help_docs/uc_mode.md

Grid docs are here: SeleniumBase Selenium Grid Documentation (But you can't use the Grid with UC Mode. Pick one)

strangeh21 commented 4 months ago

There are lots of examples that use SeleniumBase UC Mode without pytest: SeleniumBase/help_docs/uc_mode.md

Grid docs are here: SeleniumBase Selenium Grid Documentation (But you can't use the Grid with UC Mode. Pick one)

Thank you for the quick response. No problem with choosing between UC and using Selenium grid. Unfortunately I was unable to get grid working with my remote webdriver.

This is the code I was trying:

if __name__ == "__main__":
    mlx = Multilogin()
    folders = mlx.get_folders()
    test_folder_id = folders.get("testsb")
    profiles = mlx.get_profiles_in_folder(test_folder_id)
    port = mlx.start_browser_profile(test_folder_id, profiles.get("testsb3"))
    with SB(browser="firefox", servername="127.0.0.1", port=f"{port}") as sb:
        sb.open("https://seleniumbase.io/realworld/login")
        sb.type("#username", "demo_user")
        sb.type("#password", "secret_pass")
        sb.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG")  # 6-digit
        sb.assert_text("Welcome!", "h1")
        sb.highlight("img#image1")  # A fancier assert_element() call
        sb.click('a:contains("This Page")')  # Use :contains() on any tag
        sb.click_link("Sign out")  # Link must be "a" tag. Not "button".
        sb.assert_element('a:contains("Sign in")')
        sb.assert_exact_text("You have been signed out!", "#top_message")
    input()

cap.py

capabilities = {
    "browserName": "firefox", 
    "platformName": "linux",
    "browserVersion": "latest",
}

With a few attempts at setting browser='remote' or changing the cap_file parameters. I got the same error every time.

Error:

  File "/home/deb/Desktop/sbtest/temp.py", line 46, in <module>
    with SB(browser="firefox", servername="127.0.0.1", port=f"{port}") as sb:
  File "/usr/lib/python3.11/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/home/deb/Desktop/sbtest/venv/lib/python3.11/site-packages/seleniumbase/plugins/sb_manager.py", line 935, in SB
    sb.setUp()
  File "/home/deb/Desktop/sbtest/venv/lib/python3.11/site-packages/seleniumbase/fixtures/base_case.py", line 14750, in setUp
    self.driver = self.get_new_driver(
                  ^^^^^^^^^^^^^^^^^^^^
  File "/home/deb/Desktop/sbtest/venv/lib/python3.11/site-packages/seleniumbase/fixtures/base_case.py", line 4031, in get_new_driver
    new_driver = browser_launcher.get_driver(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/deb/Desktop/sbtest/venv/lib/python3.11/site-packages/seleniumbase/core/browser_launcher.py", line 1597, in get_driver
    return get_remote_driver(
           ^^^^^^^^^^^^^^^^^^
  File "/home/deb/Desktop/sbtest/venv/lib/python3.11/site-packages/seleniumbase/core/browser_launcher.py", line 2011, in get_remote_driver
    driver = webdriver.Remote(
             ^^^^^^^^^^^^^^^^^
  File "/home/deb/Desktop/sbtest/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 208, in __init__
    self.start_session(capabilities)
  File "/home/deb/Desktop/sbtest/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 292, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/deb/Desktop/sbtest/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "/home/deb/Desktop/sbtest/venv/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
    raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: HTTP method not allowed

Works just fine if I run it as a normal remote webdriver using: driver = webdriver.Remote(command_executor=f"http://127.0.0.1:{port}", options=Options())

mdmintz commented 4 months ago

Grid is working for me: (http://127.0.0.1:4444/grid/console)

Screenshot 2024-06-18 at 1 11 59 PM

Grid server: sbase grid-hub start Grid node: sbase grid-node start

My args were: browser="firefox", servername="127.0.0.1", port="4444"

For the cap_file arg, I passed in the local path of SeleniumBase/examples/capabilities/mac_cap_file.py

strangeh21 commented 4 months ago

Grid is working for me: (http://127.0.0.1:4444/grid/console)

Screenshot 2024-06-18 at 1 11 59 PM

Grid server: sbase grid-hub start Grid node: sbase grid-node start

My args were: browser="firefox", servername="127.0.0.1", port="4444"

For the cap_file arg, I passed in the local path of SeleniumBase/examples/capabilities/mac_cap_file.py

Right, thanks again for this. Apologies, I think I am misunderstanding how to use this. The remote driver is at the port generated at port = mlx.start_browser_profile(test_folder_id, profiles.get("testsb3")). This I start with selenium using driver = webdriver.Remote(command_executor=f"http://127.0.0.1:{port}", options=Options()) Here are the mlx docs explaining: https://help.multilogin.com/en_US/api/selenium-automation-example

My assumption is that this is the port I use to connect to sb, SB(browser="remote", servername="127.0.0.1", port=f"{port}", cap_file="cap.py")

As far as I understand this, with multilogin you connect directly to a profile, rather than to a selenium grid. I believe this might be out of scope and it's best to contact Multilogin support to better understand how their system is setup. Thanks again for your time @mdmintz I do really appreciate it.