update code to exlude <span class="label-copy-code">Copy</span> my code: def…
- User
update code to exlude Copy
my code:def wait_for_new_message_text(): print(">>>>>>>>>>>>>>wait_for_new_message_text") base_message_xpath = '/html/body/div[2]/div/div[1]/div/div[3]/main/div[2]/div/div/div/div[4]/div[{}]/div/div[2]/div/div[1]/*' current_message_number = 1 full_message = ""
try: WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.CLASS_NAME, "typing-indicator"))) print(">> AI responding") update_drawtarget_temp("AI responding...") except TimeoutException: print('<> TimeoutException >> starting driverrefresh') driverrefresh() print(">>>>>Starting check: text_stream") try: print(">>>>>Wait until the 'text_stream' is done") WebDriverWait(driver, global_wait_time).until( EC.invisibility_of_element_located((By.CLASS_NAME, "text_stream")) ) except TimeoutException: print(">>>>>Timed out waiting for 'text_stream' to be done. Proceeding with the script.") except NoSuchElementException: print(">>>>>Unable to locate 'text_stream' element. Proceeding with the script.") finally: print(">>>>>>>>>>>>>>>>>>> 'text_stream' is done! <<<<<<<<<<<<<<<<<<<<") while True: new_message_xpath = base_message_xpath.format(current_message_number) try: error_check(driver) # Find all elements within the current message new_message_elements = WebDriverWait(driver, 2).until( EC.presence_of_all_elements_located((By.XPATH, new_message_xpath)) ) print(f"current_message_number: {current_message_number}") current_message_number += 2 print(f"future current_message_number: {current_message_number}") # Print the tag names for debugging for element in new_message_elements: print(f"Element Tag: {element.tag_name}") # Concatenate the text content of different element types message_text = "" for element in new_message_elements: element_text = get_element_text(element) if element_text: message_text += element_text + "\n" if message_text: full_message = message_text print(f"message = {message_text}") except TimeoutException: break print(f"last current_message_number: {current_message_number}") #error_check(driver) # Remove trailing whitespace and return the full message return full_message.strip() if full_message.strip() else Nonedef get_element_text(element): invisible_separator = "\u2063" if element.tag_name == 'p': return f"{element.text.strip() if element.text else ''}" elif element.tag_name == 'ul': # Handle unordered list list_items = [get_element_text(li) for li in element.find_elements(By.XPATH, "./li")] return "\n".join(f"- {item}" for item in list_items) elif element.tag_name == 'ol': # Handle ordered list list_items = [get_element_text(li) for li in element.find_elements(By.XPATH, "./li")] return "\n".join(f"{i}. {item}" for i, item in enumerate(list_items, start=1)) elif element.tag_name == 'li': # Handle list item return f"{element.text.strip() if element.text else ''}\n" else: # Handle other element types if needed return str(element.text).strip() if element.text else ""
- Assistant
To exclude
<span class="label-copy-code">Copy</span>from the code, you can update theget_element_textfunction as follows:def get_element_text(element): invisible_separator = "\u2063" if element.tag_name == 'p': return f"{element.text.strip() if element.text else ''}" elif element.tag_name == 'ul': # Handle unordered list list_items = [get_element_text(li) for li in element.find_elements(By.XPATH, "./li")] return "\n".join(f"- {item}" for item in list_items) elif element.tag_name == 'ol': # Handle ordered list list_items = [get_element_text(li) for li in element.find_elements(By.XPATH, "./li")] return "\n".join(f"{i}. {item}" for i, item in enumerate(list_items, start=1)) elif element.tag_name == 'li': # Handle list item return f"{element.text.strip() if element.text else ''}\n" elif element.tag_name == 'span' and element.get_attribute("class") == "label-copy-code": # Exclude <span class="label-copy-code">Copy</span> return "" else: # Handle other element types if needed return str(element.text).strip() if element.text else ""