kaliiiiiiiiii / Selenium-Profiles

undetected Selenium using chromedriver and emulation / device profiles
Other
255 stars 29 forks source link

No such file or directory: '/content/chromedriver_linux64.zip'> when trying to use UC mode on MacOS #28

Closed nradosevic closed 1 year ago

nradosevic commented 1 year ago

Describe the bug I am getting an error 'No such file or directory: '/content/chromedriver_linux64.zip'>' when trying to use UC mode on MacOS

To Reproduce Try to run the example code using UC=True (tested on MacOS)

python-code

from selenium_profiles.driver import driver as mydriver
from selenium_profiles.profiles import profiles
from selenium.webdriver.common.by import By  # locate elements

mydriver = mydriver()
driver = mydriver.start(profiles.Windows(), uc_driver=True)  # or .Android

# get url
driver.get('https://browserleaks.com/client-hints')  # test client hints

input("Press ENTER to exit: ")
driver.quit()  # Execute on the End!

**Expected behavior or error-message**
Browser should start opening a desired website

error-message

Traceback (most recent call last):
  File "/Users/xyz/IT Projects/selenium-profiles/main.py", line 6, in <module>
    driver = mydriver.start(profiles.Windows(), uc_driver=True)  # or .Android
  File "/Users/xyz/IT Projects/selenium-profiles/venv/lib/python3.9/site-packages/selenium_profiles/driver.py", line 87, in start
    self.driver = uc.Chrome(use_subprocess=True, options=self.options, keep_alive=True, driver_executable_path=executable_path)  # start undetected_chromedriver
  File "/Users/xyz/IT Projects/selenium-profiles/venv/lib/python3.9/site-packages/undetected_chromedriver/__init__.py", line 246, in __init__
    self.patcher.auto()
  File "/Users/xyz/IT Projects/selenium-profiles/venv/lib/python3.9/site-packages/undetected_chromedriver/patcher.py", line 127, in auto
    self.unzip_package(self.fetch_package())
  File "/Users/xyz/IT Projects/selenium-profiles/venv/lib/python3.9/site-packages/undetected_chromedriver/patcher.py", line 163, in fetch_package
    return urlretrieve('file:///content/chromedriver_linux64.zip', '/tmp/chromedriver_linux64.zip')[0]
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 239, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 517, in open
    response = self._open(req, data)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 494, in _call_chain
    result = func(*args)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1481, in file_open
    return self.open_local_file(req)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1520, in open_local_file
    raise URLError(exp)
urllib.error.URLError: <urlopen error [Errno 2] No such file or directory: '/content/chromedriver_linux64.zip'>

Environment (please complete the following information): MacOS Monterey

EDIT: It seems that the above error appears if you actaully tried running with UC flag set to false first and then changing to true. However, when you try to run the code with UC flag set to true from the beggining you face a different error:

/Users/xyz/IT Projects/selenium-profiles-test/venv/lib/python3.9/site-packages/selenium_profiles/utils/colab_utils.py:85: UserWarning: Patching undetected-chromedriver in non-google-colab
  warnings.warn('Patching undetected-chromedriver in non-google-colab')
Traceback (most recent call last):
  File "/Users/xyz/IT Projects/selenium-profiles-test/main.py", line 6, in <module>
    driver = mydriver.start(profiles.Windows(), uc_driver=True)  # or .Android
  File "/Users/xyz/IT Projects/selenium-profiles-test/venv/lib/python3.9/site-packages/selenium_profiles/driver.py", line 63, in start
    self.options = uc.ChromeOptions()  # selenium.webdriver options, https://peter.sh/experiments/chromium-command-line-switches/
UnboundLocalError: local variable 'uc' referenced before assignment
kaliiiiiiiiii commented 1 year ago

Mhh from urllib.error.URLError: <urlopen error [Errno 2] No such file or directory: '/content/chromedriver_linux64.zip'> it seemed to me like it's an issue with undetected-chromedriver itsself, but UserWarning: Patching undetected-chromedriver in non-google-colab hint me to a bug in my code.

The "patch for google-colab" modified some code of undetected-chromedriver at selenium_profiles/utils/colab_utils.py#L67. The function was executed at selenium_profiles/driver.py#L53-L61

I wonder what happens, if:

  1. You reinstall undetected-chromedriver (pip unistall undetected-chromedriver -y; pip install undetected-chromedriver) and then start with uc_driver=True
  2. You reinstall undetected-chromedriver, and then directly run it with:
    
    import undetected_chromedriver as uc
    driver = uc.Chrome(use_subprocess=True,  keep_alive=True)

get url

driver.get('https://browserleaks.com/client-hints') # test client hints

input("Press ENTER to exit: ") driver.quit() # Execute on the End!


to confirm it isn't a issue with uc

Unfortanely, I dont't have a mac device aviable, but I anyways need to update some code for google-colab (which hopefully then resolves that issue).
nradosevic commented 1 year ago

The problem is indeed in the modified patcher.py from undetected chromedriver

After I replaced this line: return urlretrieve('file:///content/chromedriver_linux64.zip', '/tmp/chromedriver_linux64.zip')[0]

whith this one: return urlretrieve(u)[0]

everything worked as it's supposed to

kaliiiiiiiiii commented 1 year ago

The problem is indeed in the modified patcher.py from undetected chromedriver

Great, see the issue. I'll correct that in some time..

For now, I think if you allready have undeteced-chromedriver installed, Selenium-Profiles doesn't ever tries to patch automatically. ==> workaround

nradosevic commented 1 year ago

Thanks. That should do it for now. Better then manually fixing undetected chromedriver each time

kaliiiiiiiiii commented 1 year ago

https://github.com/kaliiiiiiiiii/Selenium-Profiles/commit/487e32aaa54a075d76c3368fe71c429511b7d099 should fix it.