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.67k stars 929 forks source link

Add an option to specify the Chromium browser binary used #1709

Closed mdmintz closed 1 year ago

mdmintz commented 1 year ago

Add an option to specify the Chromium browser binary used

(For the browser executable, NOT the driver)

Supports the two main Chromium browsers: Chrome and Edge.

Example:

pytest test_demo_site.py --binary-location="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"

Also as an option for other Syntax Formats:

from seleniumbase import Driver
from seleniumbase import js_utils
from seleniumbase import page_actions

driver = Driver(browser="chrome", binary_location="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome")
try:
    driver.get("https://seleniumbase.io/apps/calculator")
    page_actions.wait_for_element(driver, "4", by="id").click()
    page_actions.wait_for_element(driver, "2", by="id").click()
    page_actions.wait_for_text(driver, "42", "output", by="id")
    js_utils.highlight_with_js(driver, "#output", loops=6)
finally:
    driver.quit()
mdmintz commented 1 year ago

Released in 4.12.3 - https://github.com/seleniumbase/SeleniumBase/releases/tag/v4.12.3

mingatupturndotorg commented 8 months ago

hello @mdmintz! first, thank you for the hard work on seleniumbase, it is much appreciated.

I've searched through closed issues on setting binary location and WSL, but haven't found anything too similar. I recognize this could fully be user error, but here goes.

I'm using: -WSL: Ubuntu-22.04 -chromedriver 120.0.6099.109 (installed via 'sbase get chromedriver 120', it was defaulting to installing version 114) -sbase4.22.0

Running the following: CHROME_PATH = "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" driver = Driver( uc=True, binary_location=CHROME_PATH )

results in the following error:

Warning: The Chromium binary specified is NOT valid! Expecting "chrome.exe" to be found in ['google-chrome', 'google-chrome-stable', 'chrome', 'chromium', 'chromium-browser', 'google-chrome-beta', 'google-chrome-dev', 'google-chrome-unstable', 'brave-browser', 'brave-browser-stable', 'opera', 'opera-stable'] for the browser / OS! (Will use default settings...)

It appears that sbase rightfully recognizes that I'm running code in Linux, and thus does not consider the binary location in Windows valid since it does not match the constants, but shouldn't setting binary_location overcome that expectation?

Both

'/mnt/c/Program Files/Google/Chrome Beta/Application/chrome.exe', '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'

are also set to PATH, and appear when running os.environ.get("PATH").split(os.pathsep).

From searching through other issues, this function doesn't return anything: print(detect_b_ver.chrome_on_windows_path())

Any insight or troubleshooting is appreciated, thanks again!

mdmintz commented 8 months ago

Hello @mingatupturndotorg, you mentioned that you're running on Ubuntu-22.04, yet you're trying to use a .exe file.

.exe files aren't supported on Ubuntu. You should use one from the list provided: ['google-chrome', 'google-chrome-stable', 'chrome', 'chromium', 'chromium-browser', 'google-chrome-beta', 'google-chrome-dev', 'google-chrome-unstable', 'brave-browser', 'brave-browser-stable', 'opera', 'opera-stable'] (Those files don't have a file extension.)

For a Linux system, those are the supported binaries to choose from. Using a different one would lead to errors, so that is prevented during binary selection.

mingatupturndotorg commented 8 months ago

Thanks @mdmintz, that's a fair response. To clarify my use case or expected behavior - when using undetected-chromedriver, I could run the following script in Ubuntu with WSL:

CHROME_PATH = '/mnt/c/Program Files/Google/Chrome Beta/Application/chrome.exe'

options = uc.ChromeOptions()
options.binary_location = CHROME_PATH
driver = uc.Chrome(options=options)

and a headful Chrome installed in Windows would appear. Is that behavior possible with sbase?

mdmintz commented 8 months ago

@mingatupturndotorg I had not considered WSL (Windows Subsystem for Linux) - (https://ubuntu.com/wsl). I'll add chrome.exe as a valid option for binary_location on Linux. That may resolve your issue. (Will be in the next release.)

mingatupturndotorg commented 8 months ago

Thanks! Would a temporary fix be to add chrome.exe to valid_chrome_binaries_on_linux in constants.py?

mdmintz commented 8 months ago

@mingatupturndotorg Yes, try that.