我正在使用 c# 和 Appium + Selenium 进行 Android 模拟器自动化,并遇到了一个挑战,即查找元素需要很长时间,我在设备上尝试过,它是相同的。有人可以帮我解决这个问题吗?
能力:
appiumOptions.AddAdditionalCapability("deviceName", "emulator");
appiumOptions.AddAdditionalCapability("platformName", "Android");
appiumOptions.AddAdditionalCapability("platformVersion", "10.0");
appiumOptions.AddAdditionalCapability("appPackage", "com.app");
appiumOptions.AddAdditionalCapability("appActivity", "com.app.MainActivity");
appiumOptions.AddAdditionalCapability("automationName", "UiAutomator2");
appiumOptions.AddAdditionalCapability("app", ConfigReader.GetSetting("app"));
//appiumOptions.AddAdditionalCapability("autoGrantPermissions", true);
//appiumOptions.AddAdditionalCapability("allowSessionOverride", true);
appiumOptions.AddAdditionalCapability("disableWindowAnimation", true);
appiumOptions.AddAdditionalCapability("waitForQuiescence", false);
司机:
public static AndroidDriver<AndroidElement> _driver;
_driver = new AndroidDriver<AndroidElement>(new Uri("http://localhost:4723/wd/hub"), appiumOptions, TimeSpan.FromSeconds(300));
元素:
public AndroidElement Usernameinputbox => _driver.FindElementByAccessibilityId("username-IconInput");
Usernameinputbox.SendKeys(username);
根据Appium服务器日志,它花费了大量时间来寻找元素。输入用户名作为示例:
[WD Proxy] Matched '/element' to command name 'findElement'
[WD Proxy] Proxying [POST /element] to [POST http://127.0.0.1:8202/wd/hub/session/733a0a99-27a5-418a-8b3b-1219d55310ab/element] with body: {"strategy":"accessibility id","selector":"username-IconInput","context":"","multiple":false}
[WD Proxy] Got response with status 200: {"sessionId":"733a0a99-27a5-418a-8b3b-1219d55310ab","value":{"ELEMENT":"907853d0-a3e1-4587-8978-3e7721cdff48","element-6066-11e4-a52e-4f735466cecf":"907853d0-a3e1-4587-8978-3e7721cdff48"}}
[W3C (ad8951a0)] Responding to client with driver.findElement() result: {"element-6066-11e4-a52e-4f735466cecf":"907853d0-a3e1-4587-8978-3e7721cdff48","ELEMENT":"907853d0-a3e1-4587-8978-3e7721cdff48"}
[HTTP] <-- POST /wd/hub/session/ad8951a0-6ca8-4687-8b1d-50b8f691e971/element 200 1442 ms - 137
[HTTP]
[HTTP] --> POST /wd/hub/session/ad8951a0-6ca8-4687-8b1d-50b8f691e971/element/907853d0-a3e1-4587-8978-3e7721cdff48/value
[HTTP] {"text":"name","value":["n","a","m","e"]}
[W3C (ad8951a0)] Calling AppiumDriver.setValue() with args: [["n","a","m","e"],"907853d0-a3e1-4587-8978-3e7721cdff48","ad8951a0-6ca8-4687-8b1d-50b8f691e971"]
[WD Proxy] Matched '/element/907853d0-a3e1-4587-8978-3e7721cdff48/value' to command name 'setValue'
[Protocol Converter] Added 'value' property ["n","a","m","e"] to 'setValue' request body
[WD Proxy] Proxying [POST /element/907853d0-a3e1-4587-8978-3e7721cdff48/value] to [POST http://127.0.0.1:8202/wd/hub/session/733a0a99-27a5-418a-8b3b-1219d55310ab/element/907853d0-a3e1-4587-8978-3e7721cdff48/value] with body: {"elementId":"907853d0-a3e1-4587-8978-3e7721cdff48","text":"name","replace":false,"value":["n","a","m","e"]}
[WD Proxy] Got response with status 200: {"sessionId":"733a0a99-27a5-418a-8b3b-1219d55310ab","value":null}
[W3C (ad8951a0)] Responding to client with driver.setValue() result: null
[HTTP] <-- POST /wd/hub/session/ad8951a0-6ca8-4687-8b1d-50b8f691e971/element/907853d0-a3e1-4587-8978-3e7721cdff48/value 200 50960 ms - 14
[HTTP]
[HTTP] --> POST /wd/hub/session/ad8951a0-6ca8-4687-8b1d-50b8f691e971/element
添加 appiumOptions.AddAdditionalCapability("fullReset", true); 后发现只有登录屏幕变慢,其他屏幕现在工作正常。
尝试减少超时值
_driver = new AndroidDriver(_service, driverOptions.AppiumOptions);
_driver.ConfiguratorSetWaitForSelectorTimeout(600);
_driver.ConfiguratorSetWaitForIdleTimeout(100);