-1

When I click on a particular element on a webpage using multiple different variants of the .click() event e.g. document.querySelectorAll(selector)[pos].dispatchEvent(new MouseEvent('click', {{ bubbles: true, cancelable: true, view: window }})); or document.querySelectorAll(selector)[pos].click() the webpage doesn't load the corresponding page properly; in fact, some of these events used to work a few days ago but now don't work anymore and even if I send a reload request using javascript to the page, it doesn't show the expected dynamic elements and responsive code. Instead, it just shows a blank result where I expected information. It requires me sending a manual click event using my own mouse and clicking on the reload for the corresponding dynamic code to reappear.

Is there a way to properly and fully simulate a mouse click using javascript such that it is indistinguishable from a normal click? I've tried a few MouseEvents but perhaps I'm just configuring them wrong, or the webpage is responding differently to console executed commands.

M.Ionut
  • 187
  • 1
  • 3
  • 15
Jimmy J.K.
  • 25
  • 6
  • 1
    *Is there a way to properly and fully simulate a mouse click using javascript such that it is indistinguishable from a normal click?* No, there is no such way, and thank goodness! – connexo May 28 '22 at 23:53
  • When running the code it seems like the javascript just clicks on the already loaded material and displays already downloaded material rather than triggering a fresh request to the website. This might be why it's breaking as the click event isn't triggering a fresh information request etc from the website. Idk if that helps anyone – Jimmy J.K. May 29 '22 at 00:12
  • `MouseEvent('click', {{...}})` isn't proper object syntax – Drenai May 29 '22 at 00:28
  • Could you put just a little example of code into your question to make it clearer what is to happen on the click? And are you seeing any errors in your console and/or are you using some framework or preprocessor because the curly braces aren't making sense to me. – A Haworth May 29 '22 at 10:17
  • Basically that above isn’t correct JavaScript code because it’s python code with JavaScript used with puppeteer. I did the correct framing using the JavaScript for mouse events and the webpage was clicked however it doesn’t load like a normal click does. I can get the JavaScript code I used in the console to test it if you want however it just did the same as the puppeteer code (pretty sure I just took out the curly brackets and used normal brackets but it doesn’t really matter for the intents of this question just consider that the brackets are correct) – Jimmy J.K. May 30 '22 at 08:43
  • For JS you've got these events available: `dblclick, mousedown, mouseenter, mouseleave, mousemove, mouseout, mouseover, or mouseup`. I guess your `click` should read `mousedown` – Rene van der Lende Jun 28 '22 at 23:37

1 Answers1

0

I found a method of systematically clicking website elements largely untraceably. Basically use pyppeteer to locate the chosen element using document.querySelectorAll(*element class name*)[*position*].getBoundingClientRect()

Then click the element using pyautogui, I found using this method worked pretty well

def safeClick(baseX,baseY):
    pyautogui.moveTo(baseX,baseY,1+random.random())
    step=random.randrange(1,7)
    for i in range(step):
        baseX=baseX+random.random()
        pyautogui.moveTo(baseX,baseY)
    pyautogui.mouseDown(baseX,baseY)
    time.sleep(random.random())
    pyautogui.mouseUp(baseX,baseY)

Remember that in most browsers the getBoundingClientRect() coordinates are not exactly the same as the pyautogui coordinates, you'll need to use my answer here how to find offsets in any browser configuration.

EDIT: Note this answer works in python, if you're trying to use javascript I'm pretty sure it's largely impossible

Jimmy J.K.
  • 25
  • 6
  • 1
    Why come up with a Python solution when you tagged your question HTML, CSS, JAVASCRIPT? This will get both your question and answer downvoted/closed. Just a heads-up... – Rene van der Lende Jun 28 '22 at 23:40
  • Yes you are right, I'm not marking this as the final answer as there is plenty of room for improvement like you have said. However from reading around it does seem as though using client sided javascript executed within the browser is highly vulnerable to being detected by the website. I do believe you are right and there is a potential answer in other languages however this method offers some clues/a potential answer to an otherwise difficult question – Jimmy J.K. Jun 29 '22 at 09:45