admin管理员组文章数量:1400660
Just want to my Python script to indicate if there is a conversation happened or not. I tried this but didn't work. The script skips even if there is no conversation for a messaging function. I would like to know both indication, if conversation happened, or didn't happened ever before.
def has_existing_conversation(driver):
"""
Check if there's an existing conversation with the user by looking for
specific indicators like message bubbles, timestamps, or the state of the "Message" button.
Returns True if an existing conversation is detected.
"""
try:
# Check for the presence of message bubbles (indicators of an existing conversation)
message_bubble_indicators = [
"//div[contains(@class, 'x1i10hfl') and contains(@class, 'x1q0g3np') and contains(@class, 'x1n2onr6')]", # Message bubbles
"//div[contains(@class, 'x1i10hfl') and contains(@class, 'x1q0g3np') and contains(@class, 'x1iorvi4')]", # Message containers
"//div[contains(@class, 'x1i10hfl') and contains(@class, 'x1q0g3np') and contains(@class, 'x1n2onr6') and contains(@class, 'x1iorvi4')]", # Combined check
"//div[@class='x1n2onr6']", # Additional check for message bubbles
"//div[@class='x6s0dn4 x1bs97v6 x1q0q8m5 xso031l x9f619 x78zum5 x1q0g3np xr931m4 xat24cr x4lt0of x1swvt13 x1pi30zi xh8yej3']", # Chat history container
]
for indicator in message_bubble_indicators:
elements = driver.find_elements(By.XPATH, indicator)
if elements and len(elements) > 0:
log_event(f"Conversation detected: Found {len(elements)} message bubbles matching {indicator}")
return True
# Check for timestamps (indicators of an existing conversation)
timestamp_indicators = [
"//time", # Timestamps
"//div[contains(text(), 'Yesterday')]", # Date headers
"//div[contains(text(), 'Today')]", # Date headers
]
for indicator in timestamp_indicators:
elements = driver.find_elements(By.XPATH, indicator)
if elements and len(elements) > 0:
log_event(f"Conversation detected: Found {len(elements)} timestamps matching {indicator}")
return True
# Check the state of the "Message" button
try:
message_button = driver.find_element(By.XPATH, "//div[contains(text(), 'Message')]")
if message_button and message_button.text.strip() == "Message":
log_event("No existing conversation detected: 'Message' button is present.")
return False
elif message_button and message_button.text.strip() in ["Send Message", "Reply"]:
log_event("Existing conversation detected: 'Message' button text indicates a prior conversation.")
return True
except NoSuchElementException:
pass
# Check for the presence of a "New Message" indicator (no conversation)
try:
new_message_indicator = driver.find_element(By.XPATH, "//div[contains(text(), 'New Message')]")
if new_message_indicator:
log_event("No existing conversation detected: 'New Message' indicator is present.")
return False
except NoSuchElementException:
pass
# If none of the above checks found anything, assume no existing conversation
log_event("No existing conversation detected: No message bubbles, timestamps, or specific button state found.")
return False
except (NoSuchElementException, TimeoutException, Exception) as e:
log_event(f"Error checking for existing conversation: {str(e)}")
# In case of error, assume no conversation to avoid skipping users unnecessarily
return False
本文标签: instagramHow to indicate Python about current chat historyStack Overflow
版权声明:本文标题:instagram - How to indicate Python about current chat history? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744258983a2597618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论