'How to set timeout for chromedriver Appium

I am using python-appium client for my hybrid android application, how I can set timeout for exceptions, like NoSuchElement exception and etc., because if I switch to WebView and there is exception Appium python client don't raise exception and just infinity wait.

Appium logs:

[AndroidUiautomator2Driver@15f5 (2ed5ae5d)] Driver proxy active, passing request on via HTTP proxy [debug] [WD Proxy] Matched '/session/2ed5ae5d-e315-4ddc-a9a4-531d750fb59e/element' to command name 'findElement' [debug] [WD Proxy] Proxying [POST /session/2ed5ae5d-e315-4ddc-a9a4-531d750fb59e/element] to [POST http://127.0.0.1:8000/session/11911a8ab974feacce5cf9e170e22ee2/element] with body: {"using":"xpath","value":"//[@class="css-18v40m9 3"]"} [WD Proxy] Got response with status 404: {"value":{"error":"no such element","message":"no such element: Unable to locate element: {"method":"xpath","selector":"//[@class="css-18v40m9 3"]"}\n (Session info: chrome=91.0.4472.114)","stacktrace":"0 chromedriver_mac64_v91.0.4472.101 0x0000000100e52649 chromedriver_mac64_v91.0.4472.101 + 2741833\n1 chromedriver_mac64_v91.0.4472.101 0x0000000101508fb3 chromedriver_mac64_v91.0.4472.101 + 9781171\n2 chromedriver_mac64_v91.0.4472.101 0x0000000100bdf308 chromedriver_mac64_v91.0.4472.101 + 172808\n3 chromedriver_mac64_v91.0.4472.101 0x0000000100c1341b chromedriver_mac64_v91.0.4472.101 + 386075\n4 chromedriver_mac64_v91.0.4472.101 0x0000000100c440c4 chromedriver_mac64_v91.0.4472.101 + 585924\n5 chromedriver_mac64_v91.0.4472.101 0x0000000100c3059d chromedriver_mac64_v91.0.4472.101 + 505245\n6 chromedriver_mac64_v91.0.4472.101 0x0000000100c41e94 chromedriver_mac64_v91.0.4472.101 + 577172\n7 chromedriver_mac64_v91.0.4472.101 0x0000000100c30863 chromedriver_mac64_v91.0...



Solution 1:[1]

Using selenium's WebDriverWait you could wait for the element's presence, or its visibility, or for it to be clickable. More information here: Selenium - wait until element is present, visible and interactable

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, '...'))) 

Solution 2:[2]

using this method:

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Water Man
Solution 2 h.j