Skip to main content
  1. About
  2. For Teams
Asked
Modified today
Viewed 37 times
0

i have a script that

  1. logs in
  2. searches for a name
  3. 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.

New contributor
Vib3 Ang3l is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • what's the exception you catch there when the click fails? You should probably also include the markup after the option you want appears. (right-click, choose "Inspect", and copy and paste the surrounding markup there...)
    browsermator
    –  browsermator
    2025-10-14 16:50:47 +00:00
    Commented 10 hours ago

0

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.