如何设置chromedriver Appium超时时间



我正在使用python- Appium客户端为我的混合android应用程序,我如何设置超时异常,如NoSuchElement异常等,因为如果我切换到WebView,有异常Appium python客户端不会引发异常,只是无限等待。

Appium日志:

[AndroidUiautomator2Driver@15f5 (2ed5ae5d)]驱动代理激活,通过HTTP代理传递请求[调试][WD Proxy]匹配'/session/2ed5ae5d-e315-4ddc-a9a4-531d750fb59e/element'到命令名'findElement'[调试][WD Proxy]代理[POST/session/2ed5ae5d-e315-4ddc-a9a4-531d750fb59e/element]到[POST http://127.0.0.1:8000/session/11911a8ab974feacce5cf9e170e22ee2/element], body: {"using" xpath","value";//[@class="css-18v40m9 3"]"}[WD Proxy]得到响应状态404:{"value"; {"error":"no such element", message"; ";no such element:无法定位元素:{" method";xpath"; "selector"; "//[@class="css-18v40m9 3"]"}n(会话信息:Chrome =91.0.4472.114)","stacktrace";0 chromedriver_mac64_v91.0.4472.101 0x0000000100e52649 chromedriver_mac64_v91.0.4472.101 + 2741833n1 chromedriver_mac64_v91.0.4472.101 0x0000000101508fb3 chromedriver_mac64_v91.0.4472.101 + 9781171n2 chromedriver_mac64_v91.0.4472.101 + 172808n3 chromedriver_mac64_v91.0.4472.101 0x0000000100c1341b chromedriver_mac64_v91.0.4472.101 + 386075n4Chromedriver_mac64_v91.0.4472.101 0x0000000100c440c4 Chromedriver_mac64_v91.0.4472.101 + 585924n5 Chromedriver_mac64_v91.0.4472.101 0x0000000100c3059d Chromedriver_mac64_v91.0.4472.101 + 505245n6 Chromedriver_mac64_v91.0.4472.101 + 577172n7 Chromedriver_mac64_v91.0.4472.101 0x0000000100c30863 Chromedriver_mac64_v91.0.4472.101 0x0000000100c30863

使用selenium的WebDriverWait,你可以等待元素的出现,或者它的可见性,或者它是可点击的。更多信息在这里:硒-等待,直到元素出现,可见和可交互

from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
desired_caps = {
"platformName": "Android",
"deviceName": "emulator-5554",
"automationName": "UiAutomator2",
"platformVersion": "11",
"appPackage": "com.google.android.youtube",
"appActivity": "com.google.android.youtube.HomeActivity",
'autoGrantPermissions': 'true',
"newCommandTimeout": "86400"
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# after 120 seconds, if the condition isn't validated, a timeout exception will occur
# wait for the element's presence
WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.XPATH, '//android.widget.Button[@resource-id="com.google.android.youtube:id/random_btn"]'))) 
# wait for the element's visiblity
# WebDriverWait(driver, 120).until(EC.visibility_of_element_located((By.XPATH, '...'))) 
# wait for the element to be clickable
# WebDriverWait(driver, 120).until(EC.element_to_be_clickable((By.XPATH, '...'))) 

使用此方法:

self.driver.implicitly_wait(5) # waits 5 seconds

https://appium.io/docs/en/commands/session/timeouts/implicit-wait/

最新更新