microsoft / WinAppDriver

Windows Application Driver
MIT License
3.7k stars 1.41k forks source link

NoSuchElementException: An element could not be located on the page using the given search parameters #1888

Open hiransasidharan opened 1 year ago

hiransasidharan commented 1 year ago

Hello,

I am facing this issue when the following code is being executed. But this is not consistent. Sometimes the element is returned with no error. Sometimes the error is throwing. We are using pytest and WinAppDriver for automation of a win32 application.

Using :

def click_element_in_toolbar(self, element_index):
    """Click element in the toolbar"""
    review_window_toolbar_children = self.driver.find_element_by_accessibility_id(REVIEW_WINDOW_TOOLBAR_ID).find_elements_by_xpath(ALL_CHILDREN_PATH)
    time.sleep(2)
    review_window_toolbar_children[element_index].click()
Shakevg commented 1 year ago

@hiransasidharan Try to use wait https://www.selenium.dev/documentation/webdriver/waits/#implicit-wait

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def click_element_in_toolbar(self, element_index):
    review_window_toolbar = self.driver.find_element_by_accessibility_id(REVIEW_WINDOW_TOOLBAR_ID)
    wait = WebDriverWait(review_window_toolbar, 10)
    review_window_toolbar_children = wait.until(EC.presence_of_all_elements_located((By.XPATH, ALL_CHILDREN_PATH)))
    while len(review_window_toolbar_children) < element_index:
        review_window_toolbar_children = wait.until(EC.presence_of_all_elements_located((By.XPATH, ALL_CHILDREN_PATH)))
    time.sleep(2)
    review_window_toolbar_children[element_index].click()