-
Notifications
You must be signed in to change notification settings - Fork 1.4k
How do I set pass ChromeOptions using Driver(uc=True)? #2482
-
How do I set pass ChromeOptions
using Driver(uc=True)
from seleniumbase import Driver
from sbvirtualdisplay import Display
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
with Display(visible=0, size=(1440, 1880)):
driver = Driver(
uc=True,
browser='chrome',
cap_string=chrome_options.to_capabilities()
)
driver.get('https://www.website.com/')
This code is to proof that these ChromeOptions are not being passed because website is still loading images.
Beta Was this translation helpful? Give feedback.
All reactions
Options are passed directly into the Driver()
on initialization:
For example, there's already an option to block images with block_images=True
:
cap_string
is only for Selenium Grid use with compatible grids. And the Selenium Grid isn't compatible with UC Mode, which means you can't use cap_string
with UC Mode either.
Here's a full list of options available today:
Driver( browser=None, # Choose from "chrome", "edge", "firefox", or "safari".
Replies: 7 comments 19 replies
-
Options are passed directly into the Driver()
on initialization:
For example, there's already an option to block images with block_images=True
:
cap_string
is only for Selenium Grid use with compatible grids. And the Selenium Grid isn't compatible with UC Mode, which means you can't use cap_string
with UC Mode either.
Here's a full list of options available today:
Driver( browser=None, # Choose from "chrome", "edge", "firefox", or "safari". headless=None, # The original headless mode for Chromium and Firefox. headless2=None, # Chromium's new headless mode. (Has more features) headed=None, # Run tests in headed/GUI mode on Linux, where not default. locale_code=None, # Set the Language Locale Code for the web browser. protocol=None, # The Selenium Grid protocol: "http" or "https". servername=None, # The Selenium Grid server/IP used for tests. port=None, # The Selenium Grid port used by the test server. proxy=None, # Use proxy. Format: "SERVER:PORT" or "USER:PASS@SERVER:PORT". proxy_bypass_list=None, # Skip proxy when using the listed domains. proxy_pac_url=None, # Use PAC file. (Format: URL or USERNAME:PASSWORD@URL) multi_proxy=False, # Allow multiple proxies with auth when multi-threaded. agent=None, # Modify the web browser's User-Agent string. cap_file=None, # The desired capabilities to use with a Selenium Grid. cap_string=None, # The desired capabilities to use with a Selenium Grid. recorder_ext=None, # Enables the SeleniumBase Recorder Chromium extension. disable_js=None, # Disable JavaScript on websites. Pages might break! disable_csp=None, # Disable the Content Security Policy of websites. enable_ws=None, # Enable Web Security on Chromium-based browsers. disable_ws=None, # Reverse of "enable_ws". (None and False are different) enable_sync=None, # Enable "Chrome Sync" on websites. use_auto_ext=None, # Use Chrome's automation extension. undetectable=None, # Use undetected-chromedriver to evade bot-detection. uc_cdp_events=None, # Capture CDP events in undetected-chromedriver mode. uc_subprocess=None, # Use undetected-chromedriver as a subprocess. log_cdp_events=None, # Capture {"performance": "ALL", "browser": "ALL"} no_sandbox=None, # (DEPRECATED) - "--no-sandbox" is always used now. disable_gpu=None, # (DEPRECATED) - GPU is disabled if not "swiftshader". incognito=None, # Enable Chromium's Incognito mode. guest_mode=None, # Enable Chromium's Guest mode. dark_mode=None, # Enable Chromium's Dark mode. devtools=None, # Open Chromium's DevTools when the browser opens. remote_debug=None, # Enable Chrome's Debugger on "http://localhost:9222". enable_3d_apis=None, # Enable WebGL and 3D APIs. swiftshader=None, # Chrome: --use-gl=angle / --use-angle=swiftshader-webgl ad_block_on=None, # Block some types of display ads from loading. host_resolver_rules=None, # Set host-resolver-rules, comma-separated. block_images=None, # Block images from loading during tests. do_not_track=None, # Tell websites that you don't want to be tracked. chromium_arg=None, # "ARG=N,ARG2" (Set Chromium args, ","-separated.) firefox_arg=None, # "ARG=N,ARG2" (Set Firefox args, comma-separated.) firefox_pref=None, # SET (Set Firefox PREFERENCE:VALUE set, ","-separated) user_data_dir=None, # Set the Chrome user data directory to use. extension_zip=None, # Load a Chrome Extension .zip|.crx, comma-separated. extension_dir=None, # Load a Chrome Extension directory, comma-separated. disable_features=None, # "F1,F2" (Disable Chrome features, ","-separated.) binary_location=None, # Set path of the Chromium browser binary to use. driver_version=None, # Set the chromedriver or uc_driver version to use. page_load_strategy=None, # Set Chrome PLS to "normal", "eager", or "none". use_wire=None, # Use selenium-wire's webdriver over selenium webdriver. external_pdf=None, # Set Chrome "plugins.always_open_pdf_externally":True. is_mobile=None, # Use the mobile device emulator while running tests. mobile=None, # Shortcut / Duplicate of "is_mobile". d_width=None, # Set device width d_height=None, # Set device height d_p_r=None, # Set device pixel ratio uc=None, # Shortcut / Duplicate of "undetectable". undetected=None, # Shortcut / Duplicate of "undetectable". uc_cdp=None, # Shortcut / Duplicate of "uc_cdp_events". uc_sub=None, # Shortcut / Duplicate of "uc_subprocess". log_cdp=None, # Shortcut / Duplicate of "log_cdp_events". wire=None, # Shortcut / Duplicate of "use_wire". pls=None, # Shortcut / Duplicate of "page_load_strategy". )
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi! The argument of maximize= True does not work
Beta Was this translation helpful? Give feedback.
All reactions
-
Window maximization is done after the browser is launched:
driver.maximize_window()
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
I have this error, thanks.
michael
Beta Was this translation helpful? Give feedback.
All reactions
-
There's no maximize=True
option for Driver()
initialization.
You have to use driver.maximize_window()
after the browser is launched.
Beta Was this translation helpful? Give feedback.
All reactions
-
Okay, thanks! I have another question.I want to know which browser arguments such as chromium_arg = "--lang-en" are added by default in SeleniumBase, since, as you told me, if I add the same argument twice, it can cause problems. Thanks again."
Beta Was this translation helpful? Give feedback.
All reactions
-
Search for them in seleniumbase/core/browser_launcher.py.
There are also a few specific UC Mode options set in seleniumbase/undetected/__init__.py
Beta Was this translation helpful? Give feedback.
All reactions
-
TypeError: Driver() got an unexpected keyword argument 'downloads_path'
Beta Was this translation helpful? Give feedback.
All reactions
-
Duplicate of #1557 (comment)
SeleniumBase creates the downloaded_files
folder in the working directory where pytest
is invoked. Any click-initiated downloads will go there. It's also used for any special files needed. It also holds lock files to prevent issues with multi-threading. The folder resets at the start of every new pytest
run so that past test runs don't interfere with new ones. The folder is hard-coded there to prevent issues. And since the folder is reset at the start of new pytest
runs, you wouldn't want to use any other existing folders for it.
There are lots of built-in test methods that are specially made for that folder, such as:
self.get_downloads_folder() self.get_browser_downloads_folder() self.get_path_of_downloaded_file(file) self.is_downloaded_file_present(file) self.delete_downloaded_file_if_present(file) # Duplicates: self.delete_downloaded_file(file) self.assert_downloaded_file(file) self.get_downloaded_files(regex=None, browser=False) self.get_data_from_downloaded_file(file, timeout=None, browser=False) self.assert_data_in_downloaded_file(data, file, timeout=None, browser=False)
If you need files in a different folder, use Python os
, sys
, and shutil
libraries for copying the files you need into a different folder after they get downloaded to the downloaded_files
folder.
There's also a pytest
command-line option to archive existing downloads instead of deleting them:
Line 673 in 570910c
(A copy of those downloads will go into a folder in the archived_files/
folder at the end of the pytest
run.)
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi, is there any option for multiples downloads without navigator asking?
like prefs: "download.prompt_for_download": False,
Beta Was this translation helpful? Give feedback.
All reactions
-
You can already download multiple files without a prompt. For example:
from seleniumbase import SB with SB(test=True) as sb: sb.open("https://pypi.org/project/sbvirtualdisplay/#files") for i in range(3): sb.click('div.card a[href*=".tar.gz"]') sb.sleep(2)
Beta Was this translation helpful? Give feedback.
All reactions
-
thank you!
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi, how can I open as a safe browser to download any files like .xml? using from seleniumbase import Driver
Beta Was this translation helpful? Give feedback.
All reactions
-
SeleniumBase methods already include waits, so you don't need WebDriverWait
.
Don't use execute_script()
for clicking to download something.
Instead, use a regular click, eg. driver.click(selector)
in SeleniumBase.
If that doesn't work, add --unsafely-treat-insecure-origin-as-secure=*
into chromium_arg
(replace *
with the domain).
Reference: https://stackoverflow.com/questions/78057740/chrome-122-how-to-allow-insecure-content-insecure-download-blocked/78065424#78065424
Beta Was this translation helpful? Give feedback.
All reactions
-
ok, I tried using driver = Driver(uc=True, chromium_arg="--unsafely-treat-insecure-origin-as-secure=*") or Driver(uc=True, chromium_arg="--disable-features=InsecureDownloadWarnings " ), but didn ́t work.
I also changed the download to this: driver.click("#frmPrincipal\:tablaCompRecibidos\:0\:lnkXml"); In any case, the download continues to be blocked, with the same pop-up window appearing; another way to do it?
Beta Was this translation helpful? Give feedback.
All reactions
-
Did you replace *
with the domain, as mentioned on StackOverflow? https://stackoverflow.com/a/78065424/7058266
This is not a SeleniumBase-specific issue, so if that StackOverflow solution didn't help, then there probably isn't more advice I can give you. Maybe it will work if you don't use UC Mode (if you don't need that).
Beta Was this translation helpful? Give feedback.
All reactions
-
yes, I replaced it. I was trying to use UC: (https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/uc_mode.md) because the webSite has an anti-bot service; is there any other way to use SeleniumBase and open driver in UC mode that allow me to disable thats pop up window with prefs or chrome.options?. In the "Undetectable Automation 2 video" you have 3 ways to open the chrome driver
Beta Was this translation helpful? Give feedback.
All reactions
-
You could try all those ways. You can even try sb.download_file(file_url)
to download the file directly if you know the URL, but that's all I can think of, and that: https://stackoverflow.com/a/78065424/7058266
Beta Was this translation helpful? Give feedback.
All reactions
-
Is there a way to disable videos when using SeleniumBase with SB in CDP mode? Something like this:
from selenium.webdriver import ChromeOptions
chrome_options = ChromeOptions()
chrome_options.add_experimental_option(
"prefs",
{
"profile.managed_default_content_settings.videos": 2 # 2 = Block videos
}
)
Beta Was this translation helpful? Give feedback.
All reactions
-
Block the specific content that you want to block, like in https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/raw_req_sb.py
Beta Was this translation helpful? Give feedback.
All reactions
-
hi
how can i tell seleniumbase not to close the browser after end of running the code?
in selenium we do this:
from selenium.webdriver.edge.options import Options
the_option=Options()
the_option.add_experimental_option("detach", True)
driver=webdriver.Edge(options=the_option)
Beta Was this translation helpful? Give feedback.
All reactions
-
Use a breakpoint()
- #1814 (comment)
Beta Was this translation helpful? Give feedback.