import time,subprocess,random
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
o=Options(); o.set_preference('dom.webdriver.enabled',False)
d=webdriver.Firefox(options=o)
def xdo(*a): subprocess.run(['xdotool',*map(str,a)],check=False)
try:
    d.set_window_position(0,0); d.set_window_size(1000,800)
    d.get('http://127.0.0.1:9999/geetest_page.html'); time.sleep(6)
    r=d.execute_script("""var b=document.querySelector('.geetest_btn');var q=b.getBoundingClientRect();
      return {x:window.mozInnerScreenX+q.left+q.width/2,y:window.mozInnerScreenY+q.top+q.height/2,w:q.width,h:q.height,mx:window.mozInnerScreenX,my:window.mozInnerScreenY};""")
    print('coords',r)
    x,y=r['x'],r['y']
    xdo('mousemove',int(x),int(y)); time.sleep(0.3)
    xdo('mousedown',1); time.sleep(0.2)
    for i in range(20):
        xdo('mousemove',int(x)+ (i+1)*5, int(y)+random.randint(-1,1)); time.sleep(0.03)
    time.sleep(0.2)
    xdo('mouseup',1); time.sleep(4)
    print('validate',d.execute_script('return window.validateResult'))
    print('netcount',d.execute_script('return (window.__netlog||[]).length'))
    print('netlog',d.execute_script('return JSON.stringify(window.__netlog||[]).slice(0,2500)'))
    print('res',d.execute_script("var e=document.querySelector('.geetest_result');return e?e.innerText:null"))
finally: d.quit()
