kaliiiiiiiiii / Selenium-Driverless

undetected Selenium without usage of chromedriver
https://kaliiiiiiiiii.github.io/Selenium-Driverless/
Other
412 stars 52 forks source link

find_chrome_executable() bug #215

Closed ganyu87 closed 1 month ago

ganyu87 commented 2 months ago

i write script into script.py and call it with script.php from website (not CLI)

script.py

options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
options.binary_location = "/usr/bin/google-chrome"
async with webdriver.Chrome(options=options, debug=True) as self.driver:
    await self.main()

i got this error:

File "/usr/local/bin/.pyenv/versions/3.10.14/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/bin/.pyenv/versions/3.10.14/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/www/wwwroot/45.77.173.161/mutasi/Amuba/src/BCA/bca.py", line 59, in run
    options = webdriver.ChromeOptions()
  File "/usr/local/bin/.pyenv/versions/3.10.14/lib/python3.10/site-packages/selenium_driverless/types/options.py", line 52, in __init__
    self._binary_location = find_chrome_executable()
  File "/usr/local/bin/.pyenv/versions/3.10.14/lib/python3.10/site-packages/selenium_driverless/utils/utils.py", line 38, in find_chrome_executable
    for item in os.environ.get("PATH").split(os.pathsep):
AttributeError: 'NoneType' object has no attribute 'split'

In some reason script.py which called by script.php can not get PATH environment. so the PATH value will be None. Which cause this problem Note: php => can get the PATH environment python CLI => can get the PATH environment python script called by php => can not get the PATH environment (idk why this is happend -_- )

i have no problem with my own script. because i still can fix it with:

if(os.environ.get('PATH') is None):
    if(platform.system()=='Windows'):
        os.environ["PATH"] = "" #should be Program Files and Program Files (x86) here
    else:
        os.environ["PATH"] = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"

options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
options.binary_location = "/usr/bin/google-chrome"
async with webdriver.Chrome(options=options, debug=False) as self.driver:
    await self.main()

The reason why open this issue is because of this code:

A. Minor Issue https://github.com/kaliiiiiiiiii/Selenium-Driverless/blob/88625e998c510e6db0df5ec2563d7d826f131c18/src/selenium_driverless/types/options.py#L52

above code will try to find all chrome candidates, before i put the options.binary_location

options = webdriver.ChromeOptions() # find all chrome executable candidate
options.binary_location = "/usr/bin/google-chrome" # override the candidate
async with webdriver.Chrome(options=options, debug=True) as self.driver:
    await self.main()

i think we can let self._binary_location=None first. Then we can try find candidate if self._binary_location is None when webdriver.Chrome() called. But if this is too complicated to fix, i think we can ignore it right now

B. Major Issue https://github.com/kaliiiiiiiiii/Selenium-Driverless/blob/88625e998c510e6db0df5ec2563d7d826f131c18/src/selenium_driverless/utils/utils.py#L37-L46 There is no None checking, so if PATH is not found in environment , it will cause fatal error. I think that you just forgot it, because for Windows, you already have None checking

Thanks

kaliiiiiiiiii commented 2 months ago

@ganyu87 Thanks a lot, will definitely fix it

kaliiiiiiiiii commented 1 month ago

uhh yeah let's hope I haven't put any bug in there:) @ganyu87 can you confirm this to be fixed now btw?

kaliiiiiiiiii commented 1 month ago

https://github.com/kaliiiiiiiiii/Selenium-Driverless/pull/228/commits/f518cd02dd71640ded0480a13b0eb3d592c2ac2e#diff-836cf9d9f0b0eddb6305164344d540747d3eccecf7503ecf6ed6c55bc0177ca3R283