forked from Priyanshu-hawk/ChatGPT-unofficial-api-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_notes
93 lines (69 loc) · 4.1 KB
/
dev_notes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
any type of stractrace error like :
Stacktrace:
GetHandleVerifier [0x0104EC13+23731]
(No symbol) [0x00FD4FD0]
BaseThreadInitThunk [0x772BFCC9+25]
RtlGetAppContainerNamedObjectPath [0x7748806E+238]
Shows that the synchronous code, at that specific moment, can't find the element, so it's failing
Solution : Delay the timer a more, or check if the x-path is correct, or check the browser - if any overlayfrom the chatgpt is there or not, or the chrome / chatgpt has went to power saving mode so we need to focus to the tab
needs to add error fixes and retry to the query...
-------------
# 4. Wait for response to appear and complete
print("Step 4: Waiting for response...")
try:
# First, wait for initial response
response_container_xpath = "//div[contains(@class, 'markdown prose w-full break-words')]"
initial_response = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.XPATH, response_container_xpath))
)
print("✓ Initial response detected")
# Function to check if response is complete
def is_response_complete():
try:
# Method 1: Check if the send button is enabled again
send_button = driver.find_element(By.XPATH, "//*[@data-testid='send-button']")
if send_button.is_enabled():
return True
# Method 2: Check for the presence of typing indicators
typing_indicators = driver.find_elements(By.XPATH, "//div[contains(@class, 'result-streaming')]")
if not typing_indicators:
return True
# Method 3: Check if the response text has stopped changing
current_response = driver.find_elements(By.XPATH, response_container_xpath)[-1].text
time.sleep(2) # Brief wait
new_response = driver.find_elements(By.XPATH, response_container_xpath)[-1].text
return current_response == new_response and current_response != ""
except Exception as e:
return False
# Wait for response completion with timeout
start_time = time.time()
timeout = 180 # 3 minutes timeout
last_length = 0
while time.time() - start_time < timeout:
try:
# Get current response
responses = driver.find_elements(By.XPATH, response_container_xpath)
if responses:
current_text = responses[-1].text
current_length = len(current_text)
# If text length hasn't changed for 5 seconds and response seems complete
if current_length > 0 and current_length == last_length and is_response_complete():
time.sleep(5) # Final verification wait
if current_length == len(responses[-1].text):
break
last_length = current_length
except Exception as e:
print(f"Warning: Error while checking response: {str(e)}")
time.sleep(1) # Prevent excessive CPU usage
print("✓ Response completion confirmed")
# else: # Timeout occurred
# raise TimeoutException("Response generation timed out")
except TimeoutException as te:
print(f"Timeout while waiting for response: {str(te)}")
raise
except Exception as e:
print(f"❌ Error in Step 4 (Response Detection): {str(e)}")
raise
# Short wait for UI to stabilize
time.sleep(2)
-------------------------------------