1

Undetected Chromedriver is not applying my options. The windows size is not altered and the extension is not loaded. Same problem on Linux and Windows.

from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import undetected_chromedriver as uc

options = uc.ChromeOptions()
options.add_extension(proxies_extension)
options.add_argument("--window-size=500,500")

driver = uc.Chrome(service=Service(ChromeDriverManager().install()), options=options)

It works perfectly when I substitute the above for options = webdriver.ChromeOptions() and driver = webdriver.Chrome(...

What could I possibly be doing wrong? Thanks in advance.

Ned Hulton
  • 477
  • 3
  • 12
  • 27

1 Answers1

1

You can use SeleniumBase with the Undetected Chromedriver context manager format to set the window size after the browser spins up:

First pip install seleniumbase, and then run the following script with python:

from seleniumbase import DriverContext
import time

with DriverContext(uc=True, incognito=True) as driver:
    driver.get("https://nowsecure.nl/#relax")
    driver.set_window_size(500, 500)
    time.sleep(8)

And here's another format without the context manager:

from seleniumbase import Driver
import time

driver = Driver(uc=True, incognito=True)
driver.get("https://nowsecure.nl/#relax")
driver.set_window_size(500, 500)
time.sleep(8)
driver.quit()
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • Do standard selenium commands work with this? – Ned Hulton Jul 10 '23 at 20:50
  • @NedHulton Yes, use the ``driver``. – Michael Mintz Jul 10 '23 at 20:53
  • (1) Can I use it like this to avoid the indentation: driver = DriverContext(uc=True) – Ned Hulton Jul 10 '23 at 21:51
  • (2) Can I add arguments in the conventional way like this: options = driver.ChromeOptions() – Ned Hulton Jul 10 '23 at 21:52
  • (3) Can I use all of the conventional selenium commands like this? driver.find_element(By.XPATH, x).click() driver.find_element(By.XPATH, x).send_keys(letter) driver.find_element(By.XPATH, x).get_attribute(attr) driver.get(url) – Ned Hulton Jul 10 '23 at 21:53
  • 1
    1) See https://stackoverflow.com/a/74382784/7058266 - use ``driver = Driver()`` after importing ``Driver`` instead. 2) For options, see https://github.com/seleniumbase/SeleniumBase/blob/618f12bee59f6dfb7070f3a1d6e285b4a4e7eb61/seleniumbase/plugins/driver_manager.py#L54 (Use ``chromium-arg="--ARG1,--ARG2"`` for args not covered.) 3) Yes. – Michael Mintz Jul 10 '23 at 22:07
  • Beautiful. Nice project BTW. Sorry, but can you explain (2) again, assuming that I am not using the command line. The following is wrong, I assume – Ned Hulton Jul 10 '23 at 22:27
  • options = driver.ChromeOptions() #standard arguments options.add_argument("chromium-arg=--headless=new","--disable-blink-features=AutomationControlled") # Base arguments options.add_argument("--proxy=USERNAME:PASSWORD@SERVER:PORT") – Ned Hulton Jul 10 '23 at 22:27
  • 1
    See https://stackoverflow.com/a/75699860/7058266 for the proxy. Also supports `USER:PASS@IP:PORT` format. All those ``options.add_argument`` lines are already mapped to an arg when calling ``driver = Driver()``. Automatically uses ``headless=new`` in ``uc`` mode when using ``headless=True``. – Michael Mintz Jul 10 '23 at 22:37
  • 1
    I get you, but that was really just an example. I wasn't really asking about those specifically. Just the syntax of adding arguments in general, I have about a dozen of them. I also have options.add_experimental_option( a couple of times. Thanks for clarifying this. I am sure this will takeover standard selenium eventually – Ned Hulton Jul 10 '23 at 22:42
  • So what I am doing here. options = driver.ChromeOptions() options.add_argument("chromium-arg=--headless=new","--disable-blink-features=AutomationControlled") – Ned Hulton Jul 10 '23 at 22:47
  • Details about ``DriverContext`` vs ``Driver`` vs ``SB``, etc, are answered here: https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md Options are set here: https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/core/browser_launcher.py (including experimental options). There are args for most things. The blink one is already set as needed for ``uc`` mode. For ``headless=new``, use ``Driver(headless2=True)``. – Michael Mintz Jul 10 '23 at 22:51
  • If I am too lazy to look up your equivalent for each argument and I want to mix and match, it looks like this right? driver = Driver(browser="chrome", chromium-arg="--disable-blink-features=AutomationControlled--window-size=500,500", uc=True, --proxy=USERNAME:PASSWORD@SERVER:PORT) What am I doing wrong? – Ned Hulton Jul 11 '23 at 00:18
  • `chromium-arg` values are comma-spaced. Eg: `Driver(chromium_arg='disable-blink-features="AutomationControlled",headless="new"')`. The window size must be set after the browser is launched. And `uc` mode does not support `disable-blink-features`. – Michael Mintz Jul 11 '23 at 00:44
  • driver = Driver(browser="chrome", chromium-arg='--disable-blink-features="AutomationControlled"', uc=True) ^^^^^^^^^^^^^ SyntaxError: expression cannot contain assignment, perhaps you meant "=="? It does not like the "-" symbol – Ned Hulton Jul 11 '23 at 00:53
  • 1
    It's `chromium_arg` with an underscore, not a dash. ``driver = Driver(browser="chrome", chromium_arg='disable-blink-features="AutomationControlled"')`` – Michael Mintz Jul 11 '23 at 01:09
  • Thanks. Sorry for putting you through all that! – Ned Hulton Jul 11 '23 at 01:14
  • 1
    I hope that answered your original question. :) – Michael Mintz Jul 11 '23 at 01:17
  • Everything works perfectly. Thanks! Sincerely appreciated. Just one final question/comment. The worst thing about standard Selenium is trying to make changes on-the-fly. If you have a script with hundreds of lines of code and you just want to debug or edit one process without re-running your script. I currently use this thing to make edits in command line mode: idlex.sourceforge.net but its clunky and annoying. Is it possible to run Seleniumbase, login to a website, keep the browser open and then run additional commands interactively without starting script from scratch each time? – Ned Hulton Jul 11 '23 at 04:27
  • Use the built-in debugger. ``import pdb; pdb.set_trace()`` - https://seleniumbase.com/debugging-with-the-new-pdbp-debugger/ - https://www.youtube.com/watch?v=LLnVNfMRHvs – Michael Mintz Jul 11 '23 at 06:13