mirror of
https://github.com/Priyanshu-hawk/ChatGPT-unofficial-api-selenium.git
synced 2026-06-02 06:03:34 +02:00
Enhance Browser Support, xpath update
This commit is contained in:
+28
-11
@@ -21,6 +21,17 @@ chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
|
||||
driver = webdriver.Chrome(service=service, options=chrome_options)
|
||||
helper_fn = helper_funcs.HelperFn(driver)
|
||||
|
||||
|
||||
def check_guildlines():
|
||||
guidlines_xpath = "//*[contains(text(), 'Tips for getting started')]"
|
||||
helper_fn.wait_for_element(guidlines_xpath)
|
||||
if helper_fn.is_element_present(guidlines_xpath):
|
||||
guidlines_close_xpath = "//*[contains(text(), 'Okay, let’s go')]"
|
||||
guidlines_close = helper_fn.find_element(guidlines_close_xpath)
|
||||
guidlines_close.click()
|
||||
else:
|
||||
print("No guidlines found")
|
||||
|
||||
def start_chat_gpt():
|
||||
driver.maximize_window()
|
||||
driver.get("https://chat.openai.com/chat")
|
||||
@@ -51,6 +62,9 @@ def start_chat_gpt():
|
||||
else:
|
||||
print("Already logged in")
|
||||
|
||||
#check for guidlines
|
||||
check_guildlines()
|
||||
|
||||
def make_gpt_request(text):
|
||||
|
||||
time.sleep(3)
|
||||
@@ -61,7 +75,7 @@ def make_gpt_request(text):
|
||||
text_area.send_keys(text)
|
||||
|
||||
#send button
|
||||
send_btn_xpath = "/html/body/div[1]/div[1]/div[2]/div/main/div[3]/form/div/div/button"
|
||||
send_btn_xpath = "//*[@data-testid='send-button']"
|
||||
helper_fn.wait_for_element(send_btn_xpath)
|
||||
send_btn = helper_fn.find_element(send_btn_xpath)
|
||||
time.sleep(2)
|
||||
@@ -70,7 +84,7 @@ def make_gpt_request(text):
|
||||
time.sleep(5)
|
||||
#waiting for response
|
||||
response_xpath = "//*[@class='markdown prose w-full break-words dark:prose-invert light']"
|
||||
regenrate_xpath = "//*[contains(text(), 'Regenerate response')]"
|
||||
regenrate_xpath = "//*[contains(text(), 'Regenerate')]"
|
||||
helper_fn.wait_for_element(regenrate_xpath,120)
|
||||
if helper_fn.is_element_present(response_xpath):
|
||||
response = helper_fn.find_elements(response_xpath)[-1]
|
||||
@@ -85,12 +99,15 @@ def stop_chat_gpt():
|
||||
|
||||
if __name__ == "__main__":
|
||||
start_chat_gpt()
|
||||
|
||||
while True:
|
||||
req = input("Enter text: ")
|
||||
if req == "i quit!":
|
||||
break
|
||||
resp = make_gpt_request(req)
|
||||
print(resp)
|
||||
|
||||
stop_chat_gpt()
|
||||
|
||||
try:
|
||||
while True:
|
||||
req = input("Enter text: ")
|
||||
if req == "i quit!":
|
||||
break
|
||||
resp = make_gpt_request(req)
|
||||
print(resp)
|
||||
except KeyboardInterrupt:
|
||||
print("KeyboardInterrupt detected, exiting...")
|
||||
stop_chat_gpt()
|
||||
exit(0)
|
||||
+54
-9
@@ -12,6 +12,13 @@ except ModuleNotFoundError:
|
||||
if platform == "win32":
|
||||
subprocess.call("pip install selenium", shell=True)
|
||||
|
||||
curPlt = platform
|
||||
# https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json - download -> chrome -> {versions}
|
||||
version_dict = {'linux':0,
|
||||
'darwin':2,
|
||||
'win32':4
|
||||
} # need to improve this
|
||||
|
||||
def front_version_extractor(vrsn):
|
||||
vrsn = str(vrsn).split(".")
|
||||
n_version = ""
|
||||
@@ -30,6 +37,18 @@ def get_download_version(c_version):
|
||||
if front_version_extractor(c_version) == front_version_extractor(curr_version):
|
||||
return str(curr_version[:-1])
|
||||
|
||||
def json_version_extractor(vrsn):
|
||||
r = requests.get("https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json")
|
||||
data = r.json()
|
||||
|
||||
for i in data['versions']:
|
||||
curr_version = i['version']
|
||||
|
||||
if curr_version != None:
|
||||
if front_version_extractor(vrsn) == front_version_extractor(curr_version):
|
||||
down_link = i['downloads']['chromedriver'][version_dict[curPlt]]['url']
|
||||
return (str(curr_version), down_link)
|
||||
|
||||
def versionChk():
|
||||
chrome_version = subprocess.run(['google-chrome',' --version'], capture_output=True).stdout.decode().split(" ")[2] # chrome version check
|
||||
chrome_driver_version = subprocess.run(['./chromedriver',' --version'], capture_output=True).stdout.decode().split(" ")[1] # chromeDriver check
|
||||
@@ -37,7 +56,12 @@ def versionChk():
|
||||
return get_download_version(chrome_version) == get_download_version(chrome_driver_version)
|
||||
|
||||
|
||||
curPlt = platform
|
||||
# todl = subprocess.run(['google-chrome',' --version'], capture_output=True).stdout.decode()
|
||||
# vrsn = (todl.split(" "))[2]
|
||||
# print(vrsn)
|
||||
|
||||
# print(json_version_extractor(vrsn))
|
||||
# print(get_download_version('115.0.5763.0'))
|
||||
|
||||
|
||||
def chromeDriverDownloader():
|
||||
@@ -47,19 +71,38 @@ def chromeDriverDownloader():
|
||||
try:
|
||||
todl = subprocess.run(['google-chrome',' --version'], capture_output=True).stdout.decode()
|
||||
vrsn = (todl.split(" "))[2]
|
||||
print("Downloading Chromedriver for your system of version:",get_download_version(vrsn))
|
||||
url = "https://chromedriver.storage.googleapis.com/"+get_download_version(vrsn)+"/chromedriver_linux64.zip"
|
||||
r = requests.get(url, allow_redirects=True)
|
||||
|
||||
if "chromeDriver_zips" not in os.listdir():
|
||||
print("Creating chromeDriver_zips folder")
|
||||
os.mkdir("chromeDriver_zips")
|
||||
|
||||
open("chromeDriver_zips/chromedriver_linux64.zip","wb").write(r.content)
|
||||
if front_version_extractor(vrsn) == front_version_extractor("115.0.5762.4"):
|
||||
print("[-]Error Found: manual download required, please download it or update your chrome version!!")
|
||||
exit(0)
|
||||
|
||||
if front_version_extractor(vrsn) < front_version_extractor("115.0.5763.0"):
|
||||
print("Downloading Chromedriver for your system of version:",get_download_version(vrsn))
|
||||
url = "https://chromedriver.storage.googleapis.com/"+get_download_version(vrsn)+"/chromedriver_linux64.zip"
|
||||
r = requests.get(url, allow_redirects=True)
|
||||
|
||||
if "chromeDriver_zips" not in os.listdir():
|
||||
print("Creating chromeDriver_zips folder")
|
||||
os.mkdir("chromeDriver_zips")
|
||||
|
||||
open("chromeDriver_zips/chromedriver_linux64.zip","wb").write(r.content)
|
||||
else: # if version > 115.x.x.x
|
||||
print("Downloading Chromedriver for your system of version:",json_version_extractor(vrsn)[0])
|
||||
url = json_version_extractor(vrsn)[1]
|
||||
r = requests.get(url, allow_redirects=True)
|
||||
|
||||
if "chromeDriver_zips" not in os.listdir():
|
||||
print("Creating chromeDriver_zips folder")
|
||||
os.mkdir("chromeDriver_zips")
|
||||
|
||||
open("chromeDriver_zips/chromedriver_linux64.zip","wb").write(r.content)
|
||||
except FileNotFoundError as fnf:
|
||||
print("[-]Error Found:",fnf)
|
||||
print("[+] Google Chrome is not installed, Please install it!! - https://www.google.com/chrome/")
|
||||
exit(0)
|
||||
except Exception as e:
|
||||
print("[-]Error Found:",e)
|
||||
exit(0)
|
||||
else:
|
||||
if versionChk():
|
||||
print("[+] 'Google Chrome' and 'Chromedriver' Version Matched!!")
|
||||
@@ -132,4 +175,6 @@ chromeDriverDownloader()
|
||||
|
||||
subprocess.call
|
||||
|
||||
print("Unzipping chromedriver")
|
||||
|
||||
from unzipper import *
|
||||
+12
-3
@@ -3,17 +3,26 @@ from sys import platform
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
def move_to_base_project():
|
||||
if platform == "linux" or platform == "darwin":
|
||||
subprocess.call("mv chromedriver*/* .",shell=True)
|
||||
if platform == "win32":
|
||||
subprocess.call("move chromedriver*\\* .",shell=True)
|
||||
|
||||
def sys_chk_with_path():
|
||||
if platform == "linux":
|
||||
with ZipFile("chromeDriver_zips/chromedriver_linux64.zip") as toUnzip:
|
||||
toUnzip.extractall()
|
||||
toUnzip.extractall('.')
|
||||
move_to_base_project()
|
||||
subprocess.call("chmod +x chromedriver",shell=True)
|
||||
if platform == "win32":
|
||||
with ZipFile("chromeDriver_zips\\chromedriver_win32.zip") as toUnzip:
|
||||
toUnzip.extractall()
|
||||
toUnzip.extractall('.')
|
||||
move_to_base_project()
|
||||
if platform == "darwin":
|
||||
with ZipFile("chromeDriver_zips/chromedriver_mac64.zip") as toUnzip:
|
||||
toUnzip.extractall()
|
||||
toUnzip.extractall('.')
|
||||
move_to_base_project()
|
||||
subprocess.call("chmod +x chromedriver",shell=True)
|
||||
|
||||
if platform == "linux" or platform == "darwin":
|
||||
|
||||
Reference in New Issue
Block a user