cztomczak / cefpython

Python bindings for the Chromium Embedded Framework (CEF)
Other
3.02k stars 469 forks source link

SendMouseWheelEvent does not work on subsequent page loads - short repro attached #649

Open Neori12 opened 1 year ago

Neori12 commented 1 year ago

I had been having an issue with SendMouseWheelEvent, which does not seem to work in subsequent page loads (unless the browser is interferred with actual mouse scroll). Below is a short reproducible python script. "errorrep" is the name of the python script. Please set the script name to be "errorrep.py" as I'm using the script filename itself in certain lines of code. Thank you so much for any aid in sorting this out!

"""
SendMouseWheelEvent is correctly performed on the first page load. On any pageloads after that, it does not seem to work, but it works again when the actual mouse device performs a scroll on the webpage

'# test 1' which does a javascript code 'scrollBy', works on every pageload.
'# test 2' which does a SendMouseWheelEvent, only works on first page load.

"""

from cefpython3 import cefpython as cef
import time
from threading import Thread
import errorrep

mybrowser = None

def main():
    cef.Initialize()
    browser = cef.CreateBrowserSync(url="https://stackoverflow.com/")
    browser.SetClientHandler(LoadHandler())
    errorrep.mybrowser = browser
    cef.MessageLoop()
    del browser
    cef.Shutdown()

class LoadHandler(object):
    def OnLoadingStateChange(self, browser, is_loading, **_):
        if not is_loading:
            Thread(target=test).start()

def test():
  print("Page loading is complete!")
  for i in range(5):
    print('scrolling')
    # test 1
    errorrep.mybrowser.ExecuteJavascript(jsCode="""scrollBy(0,25)""")
    # test 2
    # errorrep.mybrowser.SendMouseWheelEvent(0,0,0,-120, cef.EVENTFLAG_NONE)
    time.sleep(1)
  print('opening new page, clicking on the logo link')
  errorrep.mybrowser.ExecuteJavascript(jsCode="""document.getElementsByClassName('s-topbar--logo js-gps-track')[0].click();""")

if __name__ == '__main__':
    main()

cefpython version is 66.1