在使用airtest进行web自动化的时候,遇到了不少的问题,总结了如何定位解决的。
问题一:unknown error: cannot find Chrome binary
具体报错信息:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
具体代码:
__author__ = "Bob"
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebChrome
driver = WebChrome()
driver.implicitly_wait(20)
driver.get("https://www.baidu.com")
定位一:查阅了一些资料,原因是chromedriver与chrome版本不匹配
解决办法:
1.首先是打开chrome,查看版本
2.在googlechromelabs.github.io/chrome-for-… 下载对应的chromedriver的版本号,放在chrome浏览器的安装目录
然而,下载对应的chromedriver后还是报错
强指定路径,问题解决,具体代码实现
__author__ = "AirtestProject"
from airtest.core.api import *
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from airtest_selenium.proxy import WebChrome
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
driver = WebChrome(options=opt,executable_path=r"C:\Program Files\Google\Chrome\Application\chromedriver.exe")
遗留问题
将chromedriver.exe配置到path环境变量中,selenium是可以成功,但是airtest还是报错,只能强制指定路径
selenium代码检测chromedriver:
import time
def main():
b=webdriver.Chrome()
b.get('http://www.baidu.com')
time.sleep(5)
b.quit()
if __name__ == '__main__':
main()
遗留问题解决
selenium是可以成功,airtest还是报错的原因是airtest在读取chromedriver.exe时,是读取的是自己目录下的chromedriver而不是新安装的。airtest自身携带的chromedriver目录是位于D:\AirtestIDE
解决方法: 将chrome对应的chromedriver的版本,直接把D:\AirtestIDE里的chromedriver替换即可
问题二:网页元素检索无法使用
看到airtest的官网发布说明是因为Chrome 111.0.5563 ~113.0.5672 暂不支持控件检索和录制功能 目前该问题还没解决,自行回退到版本109版本,问题得到解决。PS:要禁止自动更新chrome到新版本
具体详见官方说明:
问题三:使用driver.airtest_touch,无法截图
原因除了chrome版本的问题外,Chrome 111.0.5563 ~113.0.5672 暂不支持控件检索和录制功能
另外需要将缩放设置为100%,显示分辨率设置为1080P
具体操作方法为:
1.在桌面,点击鼠标右键
2.选择【显示设置】
3.将缩放设置为100%,显示分辨率设置为1080P