Throwing Errors #1729
Unanswered
codenonsense
asked this question in
Q&A
Throwing Errors
#1729
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Traceback (most recent call last): File "C:\Users\ramin\AppData\Local\Programs\Python\Python312\Lib\site-packages\undetected_chromedriver_init_.py", line 843, in del File "C:\Users\ramin\AppData\Local\Programs\Python\Python312\Lib\site-packages\undetected_chromedriver_init_.py", line 798, in quit OSError: [WinError 6] The handle is invalid this error coming from code
this error coming from this code
`
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import undetected_chromedriver as uc
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
import pathlib
import speech_recognition as sr
import pyttsx3
ScriptDir = pathlib.Path().absolute()
url = "https://flowgpt.com/chat"
chrome_option = Options()
user_agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2'
chrome_option.add_argument(f'user-agent={user_agent}')
chrome_option.add_argument('--profile-directory=Default')
chrome_option.add_argument("--headless=new")
chrome_option.add_argument(f'user-data-dir={ScriptDir}\chromedata')
service = Service(ChromeDriverManager().install())
driver = uc.Chrome()
webdriver.Chrome(service=service, options=chrome_option)
driver.maximize_window()
driver.get(url)
chatNumber = 3
def speak(text):
engine = pyttsx3.init()
id = r'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0'
engine.setProperty('voice', id)
engine.say(text)
engine.runAndWait()
r = sr.Recognizer()
def takeCommand():
with sr.Microphone() as source:
try:
r.pause_threshold = 1
audio = r.listen(source, 0, 8)
query = r.recognize_google(audio, language='en-in')
query = query.lower()
print("Recognizing...")
print(f"Ramin ==>: {query}")
return query
except sr.UnknownValueError:
return ""
except sr.RequestError as e:
return("Could not request results; {0}".format(e))
def Checker():
global chatNumber
for i in range(1, 1000):
if i % 2 != 0:
try:
chatNumber = str(i)
xpath = f"/html/body/div[1]/main/div[3]/div/div[2]/div/div[3]/div[2]/div/div[1]/div/div[{chatNumber}]/div/div/div/div"
driver.find_element(by=By.XPATH, value=xpath)
except:
print(f"the next chat number is : {i}")
chatNumber = str(i)
break
def websiteOpener():
while True:
try:
Xpath = "/html/body/div[1]/main/div[3]/div/div[2]/div/div[3]/div[2]/div/div[2]/div[2]/div[3]/div/textarea"
driver.find_element(by=By.XPATH, value=Xpath)
print("Site Loaded...")
break
except:
pass
def PopupCloser():
while True:
try:
Xpath = "/html/body/div[1]/main/div[3]/div/div[2]/div/div[3]/div[2]/div/div[2]/div[2]/div[3]/div/textarea"
driver.find_element(by=By.XPATH, value=Xpath)
break
except:
pass
def sendMessage(Query):
Xpath = "/html/body/div[1]/main/div[3]/div/div[2]/div/div[3]/div[2]/div/div[2]/div[2]/div[3]/div/textarea"
driver.find_element(by=By.XPATH, value=Xpath).send_keys(Query)
sleep(0.8)
Xpath2 = "/html/body/div[1]/main/div[3]/div/div[2]/div/div[3]/div[2]/div/div[2]/div[2]/div[3]/button"
driver.find_element(by=By.XPATH, value=Xpath2).click()
def resultScraper():
global chatNumber
chatNumber = str(chatNumber)
xpath = f"/html/body/div[1]/main/div[3]/div/div[2]/div/div[3]/div[2]/div/div[1]/div/div[{chatNumber}]/div/div/div/div"
Text = driver.find_element(by=By.XPATH, value=xpath).text
ChatNumberNew = int(chatNumber) + 2
chatNumber = ChatNumberNew
return Text
def waitfortheanswer():
sleep(2)
XPath = '/html/body/div[1]/main/div[3]/div/div[2]/div/div[3]/div[2]/div/div[2]/div[1]/div/button'
while True:
try:
driver.find_element(by=By.XPATH, value=XPath)
except:
break
websiteOpener()
Checker()
while True:
Query = takeCommand()
if "exit" in Query:
break
elif len(str(Query)) <= 5:
pass
else:
sendMessage(Query)
waitfortheanswer()
Text = resultScraper()
speak(Text)
`
first sometimes this thing works but after some time it throws this error and stops
Beta Was this translation helpful? Give feedback.
All reactions