Home > Software design >  permission issue while i am opening chrome webdriver into function in python
permission issue while i am opening chrome webdriver into function in python

Time:09-04

I am opening chrome webdriver in python.It is working fine when i am not opening via function.With function is say some permission issue and i have administration access :-

url=['https://www.youtube.com/results?search_query=free code camp']

def main1():

    try:
        driver = webdriver.Chrome('chromedriver.exe')
        driver.get(url[0])
        driver.implicitly_wait(10)

    except Exception as e:
        print(e)
main1()

Without function is it working. Webdriver open and after few second i got this issue :-

Traceback (most recent call last):

  File "C:\Users\asus\anaconda3\envs\Jinja2_sample1\lib\subprocess.py", line 788, in __del__
    self._internal_poll(_deadstate=_maxsize)
  File "C:\Users\asus\anaconda3\envs\Jinja2_sample1\lib\subprocess.py", line 1055, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
PermissionError: [WinError 5] Access is denied

CodePudding user response:

Try using the webdriver-manager library. It simplifies the work with drivers.

Here is the code that worked for me:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

url=['https://www.youtube.com/results?search_query=free code camp']

def main1():

    try:
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
        driver.get(url[0])
        driver.implicitly_wait(10)

    except Exception as e:
        print(e)

main1()

Also I used venv, not Anaconda

CodePudding user response:

Considering that your code is running without the function, the issue might be caused by the garbage collector.

As per this discussion Google chrome closes immediately after being launched with selenium.

To prevent this, we can return the driver object from the function

import time
from selenium import webdriver


url = ['https://stackoverflow.com']
def main1():
    try:
        driver = webdriver.Chrome('chromedriver.exe')
        driver.get(url[0])
        driver.implicitly_wait(10)
    except Exception as e:
        print(e)

    return driver

driver = main1()
time.sleep(1)
driver.quit()

Try updating your selenium version, it seems to work for v 3.141.0 in conda

  • Related