1. 程式人生 > 實用技巧 >Airtest+python自動化-繞過驗證碼

Airtest+python自動化-繞過驗證碼

今天在完成公司UI自動化時候碰到這樣一個情況,使用chrome開啟square系統後頁面一直在載入,沒有更新出來,同樣的chrome領一個常用的卻能夠載入
在這裡插入圖片描述
圖1為無法載入
在這裡插入圖片描述
圖2為正常載入

此時開始排查問題,首先發現airtest自動載入頁面是一個default頁面,該頁面沒有賬號登陸,定位問題可能和賬號許可權有關,所以選擇了繞過Google登陸,直接開啟square頁面
新增如下程式碼
options=webdriver.ChromeOptions()
options.add_argument("–user-data-dir=C:\Users\Mars.CNL-4P88XT2\AppData\Local\Google\Chrome\User Data")

driver = webdriver.Chrome(chrome_options=options)
driver.implicitly_wait(20)
試著執行自動化指令碼,再次出現問題

======================================================================
ERROR: runTest (app.widgets.code_runner.ide_launcher.AirtestIDECase)

Traceback (most recent call last):
File “airtest\cli\runner.py”, line 65, in runTest

File “site-packages\six.py”, line 693, in reraise
File “airtest\cli\runner.py”, line 61, in runTest
File “C:\Users\Mars.CNL-4P88XT2\Desktop\SquareSearchName.air\SquareSearchName.py”, line 13, in
driver = webdriver.Chrome(chrome_options=options)
File “D:\AirtestIDE-win-1.2.5\AirtestIDE\selenium\webdriver\chrome\webdriver.py”, line 81, in init

desired_capabilities=desired_capabilities)
File “D:\AirtestIDE-win-1.2.5\AirtestIDE\selenium\webdriver\remote\webdriver.py”, line 157, in init
self.start_session(capabilities, browser_profile)
File “D:\AirtestIDE-win-1.2.5\AirtestIDE\selenium\webdriver\remote\webdriver.py”, line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File “D:\AirtestIDE-win-1.2.5\AirtestIDE\selenium\webdriver\remote\webdriver.py”, line 321, in execute
self.error_handler.check_response(response)
File “D:\AirtestIDE-win-1.2.5\AirtestIDE\selenium\webdriver\remote\errorhandler.py”, line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.18363 x86_64)


Ran 1 test in 68.200s

FAILED (errors=1)
[Finished]

============================================================

chrome啟動了但是沒有輸入url,需要再次排查。發現chromedriver版本顯示2.37,該版本是下載airtest時自帶的版本,排查是否chromedriver和chrome版本不匹配造成chrome crash了,查詢chromedriver和chrome匹配表,發現chromedriver2.37對應的chrome版本時66,查詢本機chrome版本為86,需要先匹配版本。
這裡由於重新下載低版本的chrome需要刪除原有的chrome,可能會造成自己的資料丟失,於是只能下載了最匹配86版本的chromedriver,替換原本airtest中的chromedriver,重新啟動airtest後發現再次報錯
在這裡插入圖片描述
網上查詢發現也有網友出現類似情況,解決方案為複製user data資料,並將options改名到新地址,操作方式:複製user data為user data1
重新修改options程式碼
options=webdriver.ChromeOptions()
options.add_argument("–user-data-dir=C:\Users\Mars.CNL-4P88XT2\AppData\Local\Google\Chrome\User Data1")
driver = webdriver.Chrome(chrome_options=options)
driver.implicitly_wait(20)
儲存指令碼並啟動,問題順利解決