Dan_San
Dan_San

Reputation: 21

How to trigger a long-press JS initiator?

I am a junior programmer who recently began exploring Playwright in Python (still not familiar with JavaScript).

I am attempting to create a ChatRoom robot for non-threatening purposes (i.e., legal, personal use).

My program is able to successfully navigate through the web pages, all the way to any targeted chatroom, and sequentially type a message. However, when it moves to the send button and presses it, although the message appears in the chatroom, it is in fact not sent (I double check in another session of a separate account). The program itself does NOT return any error. This means that my script does click on the "Send" button, but the server will not accept the request. In my view, it means that a JS initiator is not being triggered. My goal is for the message typed to actually be accepted by the servers and shown to other users.

Note. For bot-detection avoidance purposes, I am moving and clicking with python_ghost_cursor.create_cursor. This package is supposed to comply with Fitts's law and with Bezier curve properties (see GitHub, python_ghost_cursor by Xetera).

import playwright
import time
from playwright.sync_api import sync_playwright
from python_ghost_cursor.playwright_sync import create_cursor
#from python_ghost_cursor.playwright_sync import path
from python_ghost_cursor.playwright_sync import install_mouse_helper
import math
import random
import pyautogui
from random import randrange

with sync_playwright() as p:

    # INITIALIZING THE ROBOT & LAUNCHING WEBDRIVER

    browser = p.chromium.launch(headless=False)
    context=browser.new_context(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0)               Gecko/20100101 Firefox/126.0")
    page = context.new_page()
    page.set_viewport_size({"width":1920,"height":1080})
    cursor=create_cursor(page)
    install_mouse_helper(page)
    page.goto("https://example.com/")
    #time.sleep(sleepy)

    page.goto("https://www.example.com/en/chat/anychat")
    sleep_2()

    [...]

    x_new=random.randint(200,500)
    y_new=random.randint(200,250)
    cursor.move_to({"x":x_new,"y":y_new})
    page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
    page.evaluate("window.scrollBy(90,115)")
    page.evaluate("window.scrollBy(90,115)")
    page.evaluate("window.scrollBy(90,115)")

    selector=page.locator('css=.ProseMirror')
    cursor.click(selector)
    selector=page.locator('css=.ProseMirror.ProseMirror-focused')
    sleep_3()
    selector.press_sequentially("message",delay=350)
    sleep_2()
    selector=page.locator('css=.chat-Messenger > div:nth-child(2)')
    selector.wait_for()
    cursor.move
    cursor.click(selector,wait_for_click=612)
    time.sleep(30)

I checked the Network interface while sending a message manually. It seems like the POST request is conditioned by the following JS initiator: long-press-event, which was published online by John Doherty under MIT license. I tried to have the the cursor perform a long click (i.e., wait_for_click=612). However, no matter my attempts, the message does not get ultimately sent.

Would anyone have a tip to solve the issue?

Upvotes: 1

Views: 45

Answers (0)

Related Questions