Skip to content

Commit

Permalink
Merge pull request #59 from lhoggatt17/patch-1
Browse files Browse the repository at this point in the history
Support user-defined driverPath
  • Loading branch information
nateshmbhat authored Jan 24, 2022
2 parents ce676b2 + a09a3bb commit 9be90fa
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions webbot/webbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Browser:
:Args:
- showWindow : If false , will run a headless browser without showing GUI window.
- proxy : Url of any optional proxy server.
- driverPath: can specify the path of an alternative chromedriver
Expand All @@ -37,7 +38,7 @@ class Browser:
- List containing all the errors which might have occurred during performing an action like click ,type etc.
"""

def __init__(self, showWindow=True, proxy=None , downloadPath:str=None, arguments=["--disable-dev-shm-usage","--no-sandbox"]):
def __init__(self, showWindow=True, proxy=None , downloadPath:str=None, driverPath:str=None, arguments=["--disable-dev-shm-usage","--no-sandbox"]):
options = webdriver.ChromeOptions()

for argument in arguments:
Expand All @@ -47,8 +48,13 @@ def __init__(self, showWindow=True, proxy=None , downloadPath:str=None, argument
absolutePath = os.path.abspath(downloadPath)
if(not os.path.isdir(absolutePath)):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), absolutePath)

options.add_experimental_option('prefs', {'download.default_directory' : absolutePath})

if driverPath is not None and isinstance(driverPath,str):
driverPath = os.path.abspath(driverPath)
if(not os.path.isdir(driverPath)):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), driverPath)

if proxy is not None and isinstance(proxy, str):
# Check if '--proxy-server' has not yet been set
Expand All @@ -59,18 +65,19 @@ def __init__(self, showWindow=True, proxy=None , downloadPath:str=None, argument
options.headless = True
options.add_argument("--headless")

driverfilename = ''
if sys.platform == 'linux' or sys.platform == 'linux2':
driverfilename = 'chrome_linux'
elif sys.platform == 'win32':
driverfilename = 'chrome_windows.exe'
elif sys.platform == 'darwin':
driverfilename = 'chrome_mac'
driverpath = os.path.join(os.path.split(__file__)[0], 'drivers{0}{1}'.format(os.path.sep, driverfilename))
if driverPath is None:
driverfilename = ''
if sys.platform == 'linux' or sys.platform == 'linux2':
driverfilename = 'chrome_linux'
elif sys.platform == 'win32':
driverfilename = 'chrome_windows.exe'
elif sys.platform == 'darwin':
driverfilename = 'chrome_mac'
driverPath = os.path.join(os.path.split(__file__)[0], 'drivers{0}{1}'.format(os.path.sep, driverfilename))

os.chmod(driverpath, 0o755)
os.chmod(driverPath, 0o755)

self.driver = webdriver.Chrome(executable_path=driverpath, options=options)
self.driver = webdriver.Chrome(executable_path=driverPath, options=options)
self.Key = Keys
self.errors = []

Expand Down

0 comments on commit 9be90fa

Please sign in to comment.