tetete

tet
mail@pastecode.io avatar
unknown
python
2 years ago
3.2 kB
2
Indexable
Never
from os import listdir
import urllib2
import requests
import os
from zipfile import ZipFile
import shutil
import glob
import subprocess
import re


def get_chrome_ver(mypath):
    return(listdir(mypath)[0])

def create_path(version):
    print (version)
    url = "https://chromedriver.storage.googleapis.com/"+str(version)+"/chromedriver_win32.zip"
    req = requests.get(url)
    filename = url.split('/')[-1]
    print(filename)
    with open(filename,'wb') as output_file:
        output_file.write(req.content)
    print('Downloading Completed')
    deleteOldDriver()
    unzipFile()

def download_file(version):
    requests.get(version)
    
def deleteOldDriver():
    path = 'C:\\automation\\chromedriver_win32\\chromedriver.exe'
    os.remove(path)
    
def unzipFile():
    zipObj = ZipFile(r'chromedriver_win32.zip', 'r')
    zipObj.extractall(r'C:\automation\chromedriver_win32')
    zipObj.close()
            
def getChromeDriverVersion():
    ver = os.popen("C:\\automation\\chromedriver_win32\\chromedriver.exe -v").read()
    return ver[re.search(r"\d",ver).start():ver.find(".")]
    
def runChromeUpdate():
    chromeUpdatePath = "C:\Program Files (x86)\Google\Update"
    subprocess.call("C:\Program Files (x86)\Google\Update\GoogleUpdate.exe", shell=True)
    
def killChrome():
    os.system('cmd /c "taskkill /im java.exe /t /f"')
    
def runChromeDriver():
    os.system('cmd /c "java -Dwebdriver.chrome.driver=C:\\automation\\chromedriver_win32\\chromedriver.exe -jar C:\\automation\\selenium-server-4.1.0.jar standalone --detect-drivers false --port 4440"')
    #subprocess.Popen("C:\\Users\\selenium\\Desktop\\sel4.bat",creationflags=subprocess.CREATE_NEW_CONSOLE)
    #subprocess.Popen(["cmd", "/c", "java -Dwebdriver.chrome.driver=C:\\automation\\chromedriver_win32\\chromedriver.exe -jar C:\\automation\\selenium-server-4.1.0.jar standalone --detect-drivers false --port 4440"],creationflags=subprocess.CREATE_NEW_CONSOLE)
    print("is subprocess works ?")
    exit()

    
        

    
def manageDriver():
    killChrome()
    response = urllib2.urlopen('http://chromedriver.storage.googleapis.com/LATEST_RELEASE')
    html = response.read()
    ver = 'C:\Program Files (x86)\Google\Chrome\Application'[0:3]
    systemVersion = int((get_chrome_ver('C:\Program Files\Google\Chrome\Application'))[0:3])
    downloadableVersion = int((html)[0:3])
    if int(getChromeDriverVersion()) < downloadableVersion:
        print(str(getChromeDriverVersion())+"is older than available version"+str(downloadableVersion))
        if downloadableVersion > systemVersion:
            print("chrome broswer is not up to date "+str(systemVersion)+" might me updated to "+str(downloadableVersion)+" Please update chrome first")
            runChromeUpdate()
            create_path(html) # normalnie to powinno zostac dodane gdy chrome zostanie zapdejtowany, ale ten apdejt tak srednio dziala
            pass
        else:
            create_path(html)
    else:
        print("chromeDriver is up to date")
            
    
manageDriver() 
#runChromeDriver()