selenium自己處理不了windows窗口,須要藉助,PyAutoit包 與autoit工具python
這裏以文件上傳窗口爲例:windows
1.安裝python pyauto包api
pip install PyAutoit
2.安裝autoit工具,下載地址以下,這裏使用的是autoit3版本工具
https://www.autoitscript.com/site/autoit/downloads/ui
3.處理文件上傳window窗口spa
須要完成如下步驟:code
1>.輸入文件路徑blog
2>.點擊,打開按鈕ip
4.文件上傳autoit python代碼實現get
1>經過auto軟件,抓取上傳窗口,屬性
2>引用python引用autoit包
import autoit #設置焦#點 autoit.control_focus("打開","[Class:Edit; instance:1]") #輸入文本 autoit.control_set_text("打開","[Class:Edit; instance:1]",r"C:\Users\Administrator\Desktop\226523.jpg") # 單擊按鈕 autoit.control_click("打開","[Class:Button; instance:1]")
5.代碼分解
以上三行代碼便可,固然,你可能不太明白這些參數都是什麼意思,那麼下邊咱們分別看一下原型。
Title:auto抓取到的Title以下圖
control:就是autoit抓出的control中的屬性。
control,在python代碼中用 "[屬性:屬性值;屬性2:屬性值2]" 表示
1>autoit.control_focus():
@api.check(2, "Window/Control could not be found") def control_focus(title, control, **kwargs): """ :param title: :param control: :param kwargs: :return: """ text = kwargs.get("text", "") ret = AUTO_IT.AU3_ControlFocus( LPCWSTR(title), LPCWSTR(text), LPCWSTR(control)) return ret
2>autoit.control_set_text():
@api.check(2, "Window/Control could not be found") def control_set_text(title, control, control_text, **kwargs): """ :param title: :param control: :param control_text: :param kwargs: :return: """ text = kwargs.get("text", "") ret = AUTO_IT.AU3_ControlSetText( LPCWSTR(title), LPCWSTR(text), LPCWSTR(control), LPCWSTR(control_text) ) return ret
3>autoit.control_click():
@api.check(2, "send click message failed") def control_click(title, control, **kwargs): """ :param title: :param text: :param control: :param button: :param clicks: :param x: :param y: :return: """ text = kwargs.get("text", "") button = kwargs.get("button", "left") clicks = kwargs.get("clicks", 1) x = kwargs.get("x", INTDEFAULT) y = kwargs.get("y", INTDEFAULT) ret = AUTO_IT.AU3_ControlClick(LPCWSTR(title), LPCWSTR(text), LPCWSTR(control), LPCWSTR(button), INT(clicks), INT(x), INT(y)) return ret
總結:
1>安裝(auit和pyautoit包)
2>抓取(auit抓取上傳窗口)
3>編寫python代碼(設置焦點,輸入文本,點擊上傳)
注意,能夠加入一些等待時間,還控制焦點與輸入間隔