pycharm 自動化測試selenium+Python3遇到的問題及解決方法(二)

1、InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified

:用By.CSS_Selector來定位組件時,#後面的id、class不能是數字、標點開頭的,否則會報錯

2 隱藏元素怎麼定位和操作(type=hidden、display:none)

定位·:跟普通元素定位一樣,難的是怎麼操作
判斷元素是否隱藏:is_displayed()
本身隱藏元素是無法操作的,但能正常定位,非要操作的話,可以用js的方法去操作,selenium的入口提供了一個可以執行js腳本
只要頁面上有的元素(dom裏面的),js都可以操作
js=’'document.getElementById(“a”).click()
driver.excute_script(js)

3、syntax error:語法錯誤

4、Selenium實現圖片文件上傳方式(解決Selenium不能上傳文件問題)

地址:https://blog.csdn.net/weixin_42024694/article/details/80080629
利用autoit工具實現
autoit官方網站:https://www.autoitscript.com/site/
步驟:(1)用AutoIt windows Info工具獲取上傳文件的文件名輸入框和打開按鈕
在這裏插入圖片描述
(2)根據(1)獲得的控件信息,打開SciTE Script Editor編輯器編寫AutoIT腳本;通過菜單欄“tools”–"go"運行,注意:運行時文件上傳窗口應處於打開狀態。

;ControlFocus("title","text",controlID) Edit1=Edit instance 1
ControlFocus("打開","","Edit1")

;Wait 10 seconds for the Upload window to appear
WinWait("[CLASS:#32770]","",10)

;Set the File Name text on the Edit field
ControlSetText("打開","","Edit1","D:\jenkins\workspace\zhsq_autotest\web\upload.png")
Sleep(2000)

;Click on the open button
ControlClick("打開","","Button1");
Sleep(2000)

(3)通過Compile Script to.exe工具將(2)的**.au3腳本生成exe程序被python調用。
在這裏插入圖片描述
(4)python調用.exe程序

import os
os.system("D:\upfile_gt.exe")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章