02月25, 2020

Mac安装selenium

selenium环境安装

pip3 install selenium

http://chromedriver.chromium.org/downloads
#将安装包加入到环境变量。以mac系统为例,将chromedriver移至/usr/bin目录下
mv ~/Downloads/chromedriver /usr/bin

Mac键盘、鼠标控制


import time
'''
使用pykeyboard发现回车不起作用,所以采用pynput
'''
def my_return():
    from pynput.keyboard import Key, Controller
    keyboard = Controller()
    keyboard.press(Key.enter)
    time.sleep(2)
    keyboard.release(Key.enter)
    keyboard.press(Key.enter)
    time.sleep(2)
    keyboard.release(Key.enter)
'''
处理web文件上传问题,自动定位到指定文件,选中确认
'''
def upload_file_mac(file):
    import pyperclip
    from pymouse import PyMouse
    from pykeyboard import PyKeyboard
    pyk = PyKeyboard()
    pym = PyMouse()
    time.sleep(2)
    #模拟快捷键Command+Shift+G 快捷进入指定路径
    print("Command+Shift+G")
    pyk.press_keys(['Command', 'Shift', 'G'])
    #点击屏幕中间
    x_dim, y_dim = pym.screen_size()
    pym.click(x_dim // 2, y_dim // 2, 1)
    time.sleep(2)
     # 输入文件全路径进去
    pyperclip.copy(file)
    print("copy="+file)
    pyk.press_keys(['Command', 'V'])
    my_return()

本文链接:https://blog.jnliok.com/post/Mac-an-zhuang-selenium.html

-- EOF --

Comments