i have a script that
- logs in
- searches for a name
- arrows down then enter (not working)
how do i fix this? It would be nice if it is completely headless
def send_snap_to_recipient(driver: webdriver.Chrome, recipient: str, image_path: str) -> bool:
"""
Finds a chat with `recipient` and sends `image_path` as a snap (media).
Returns True on apparent success.
"""
try:
RAND_SLEEP()
# Find search box (common approach)
# Try input with placeholder 'Search' or aria-label search
try:
search_input = wait_for(driver, By.XPATH, "//input[contains(@placeholder,'Search') or @aria-label='Search' or contains(@aria-label,'Search')]")
except Exception:
search_input = wait_for(driver, By.XPATH, "//input")
search_input.clear()
search_input.send_keys(recipient)
RAND_SLEEP(0.5, 1.0)
# After typing, wait for search results and click the user
# Often a list item with the username/display name will appear
# We'll try to click an element containing the recipient text
try:
result = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable(
(By.XPATH, f"//div[normalize-space(text())=, '{recipient}']"))
)
result.click()
except Exception:
# fallback: press ENTER to open first result
search_input.send_keys(Keys.RETURN)
# After typing recipient name, press Arrow Down then Enter
RAND_SLEEP(0.2, 0.5)
search_input.send_keys(Keys.ARROW_DOWN)
RAND_SLEEP(0.2, 0.5)
search_input.send_keys(Keys.RETURN)
RAND_SLEEP(0.2, 0.5)
search_input.send_keys(Keys.ENTER)
RAND_SLEEP(0.2, 0.5)
search_input.send_keys(Keys.RETURN)
RAND_SLEEP()
time.sleep(2.5)
# Now in chat view. Find the attach/upload button (paperclip or camera icon).
# Many web apps expose an <input type="file"> for uploads which is easiest to populate.
# Try to find a file input
file_input = None
try:
file_input = driver.find_element(By.XPATH, "//input[@type='file']")
except Exception:
# try different possible attributes
try:
file_input = driver.find_element(By.CSS_SELECTOR, "input[type='file']")
except Exception:
file_input = None
if file_input:
# Send file path to input
file_input.send_keys(image_path)
RAND_SLEEP(1, 2)
# After upload, there should be a send button. Try common selectors.
try:
send_btn = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Send') or @aria-label='Send']"))
)
send_btn.click()
RAND_SLEEP(0.5, 1.0)
return True
except Exception:
# Some UI shows a preview with a separate button (like "Send Snap")
try:
send_preview = driver.find_element(By.XPATH, "//button[contains(., 'Send Snap') or contains(., 'Send')]")
send_preview.click()
RAND_SLEEP()
return True
except Exception as e:
print("[send_snap] Could not find send button after upload:", e)
return False
else:
# No file input found: attempt clipboard or other flows not implemented
print("[send_snap] No <input type='file'> found — UI may require camera or custom uploader.")
return False
except Exception as e:
print(f"[send_snap] Exception sending to {recipient}: {e}")
return False
I tried many things, but none worked.