33from typing import Optional , Any , Union
44from System import Exception as CSharpException # pylint: disable=import-error
55from System import InvalidOperationException # pylint: disable=import-error
6+ from System .Runtime .InteropServices import COMException # pylint: disable=import-error
67from FlaUI .Core import Debug as FlaUIDebug # pylint: disable=import-error
78from FlaUI .Core .Exceptions import PropertyNotSupportedException # pylint: disable=import-error
89from FlaUI .Core .Exceptions import ElementNotAvailableException # pylint: disable=import-error
@@ -141,9 +142,6 @@ def _get_name_from_element(self, xpath: str):
141142
142143 Args:
143144 xpath (string): XPath identifier from element.
144-
145- Raises:
146- COMException: If node don't exist.
147145 """
148146 return self ._get_element (xpath ).Name
149147
@@ -153,9 +151,6 @@ def _get_rectangle_bounding_from_element(self, xpath: str):
153151
154152 Args:
155153 xpath (string): XPath identifier from element.
156-
157- Raises:
158- COMException: If node don't exist.
159154 """
160155 rect = self ._get_element (xpath ).BoundingRectangle
161156 return [Converter .cast_to_int (rect .X ),
@@ -207,19 +202,15 @@ def _get_element(self, xpath: str):
207202 Raises:
208203 FlaUiError: If node could not be found by xpath.
209204 """
210- try :
205+ component = self ._get_element_by_xpath (xpath )
206+ if not component and self ._timeout > 0 :
207+ time .sleep (self ._timeout / 1000 )
211208 component = self ._get_element_by_xpath (xpath )
212- if not component and self ._timeout > 0 :
213- time .sleep (self ._timeout / 1000 )
214- component = self ._get_element_by_xpath (xpath )
215209
216- if component :
217- return component
210+ if component :
211+ return component
218212
219- raise FlaUiError (FlaUiError .XPathNotFound .format (xpath ))
220-
221- except CSharpException :
222- raise FlaUiError (FlaUiError .XPathNotFound .format (xpath )) from None
213+ raise FlaUiError (FlaUiError .XPathNotFound .format (xpath ))
223214
224215 def _get_element_by_xpath (self , xpath : str ):
225216 """
@@ -232,6 +223,10 @@ def _get_element_by_xpath(self, xpath: str):
232223 return self ._automation .GetDesktop ().FindFirstByXPath (xpath )
233224 except ElementNotAvailableException :
234225 return None
226+ except COMException :
227+ return None
228+ except CSharpException :
229+ return None
235230
236231 def _find_all_elements (self , xpath : str ):
237232 """
0 commit comments