2121
2222from selenium .webdriver .common .by import By
2323from selenium .webdriver .support .ui import WebDriverWait
24- from selenium .common .exceptions import (TimeoutException , WebDriverException ,
25- InvalidArgumentException , NoSuchElementException )
24+ from selenium .common .exceptions import TimeoutException , InvalidArgumentException
2625
2726from selenium .webdriver .remote .command import Command as RemoteCommand
2827
3534from .errorhandler import MobileErrorHandler
3635from .switch_to import MobileSwitchTo
3736from .webelement import WebElement as MobileWebElement
38- from .imagelement import ImageElement
39-
40- DEFAULT_MATCH_THRESHOLD = 0.5
4137
4238# From remote/webdriver.py
4339_W3C_CAPABILITY_NAMES = frozenset ([
@@ -207,6 +203,7 @@ def find_element(self, by=By.ID, value=None):
207203 :rtype: WebElement
208204 """
209205 # if self.w3c:
206+
210207 # if by == By.ID:
211208 # by = By.CSS_SELECTOR
212209 # value = '[id="%s"]' % value
@@ -218,8 +215,6 @@ def find_element(self, by=By.ID, value=None):
218215 # elif by == By.NAME:
219216 # by = By.CSS_SELECTOR
220217 # value = '[name="%s"]' % value
221- if by == By .IMAGE :
222- return self .find_element_by_image (value )
223218
224219 return self .execute (RemoteCommand .FIND_ELEMENT , {
225220 'using' : by ,
@@ -251,9 +246,6 @@ def find_elements(self, by=By.ID, value=None):
251246 # Return empty list if driver returns null
252247 # See https://github.com/SeleniumHQ/selenium/issues/4555
253248
254- if by == By .IMAGE :
255- return self .find_elements_by_image (value )
256-
257249 return self .execute (RemoteCommand .FIND_ELEMENTS , {
258250 'using' : by ,
259251 'value' : value })['value' ] or []
@@ -370,51 +362,34 @@ def find_elements_by_android_viewtag(self, tag):
370362 """
371363 return self .find_elements (by = By .ANDROID_VIEWTAG , value = tag )
372364
373- def find_element_by_image (self , png_img_path ,
374- match_threshold = DEFAULT_MATCH_THRESHOLD ):
365+ def find_element_by_image (self , img_path ):
375366 """Finds a portion of a screenshot by an image.
376367 Uses driver.find_image_occurrence under the hood.
377368
378369 :Args:
379- - png_img_path - a string corresponding to the path of a PNG image
380- - match_threshold - a double between 0 and 1 below which matches will
381- be rejected as element not found
370+ - img_path - a string corresponding to the path of a image
382371
383- :return: an ImageElement object
372+ :return: an Element object
384373 """
385- screenshot = self .get_screenshot_as_base64 ()
386- with open (png_img_path , 'rb' ) as png_file :
387- b64_data = base64 .b64encode (png_file .read ()).decode ('UTF-8' )
388- try :
389- res = self .find_image_occurrence (screenshot , b64_data ,
390- threshold = match_threshold )
391- except WebDriverException as e :
392- if 'Cannot find any occurrences' in str (e ):
393- raise NoSuchElementException (e )
394- raise
395- rect = res ['rect' ]
396- return ImageElement (self , rect ['x' ], rect ['y' ], rect ['width' ],
397- rect ['height' ])
398-
399- def find_elements_by_image (self , png_img_path ,
400- match_threshold = DEFAULT_MATCH_THRESHOLD ):
374+ with open (img_path , 'rb' ) as i_file :
375+ b64_data = base64 .b64encode (i_file .read ()).decode ('UTF-8' )
376+
377+ return self .find_element (by = By .IMAGE , value = b64_data )
378+
379+ def find_elements_by_image (self , img_path ):
401380 """Finds a portion of a screenshot by an image.
402381 Uses driver.find_image_occurrence under the hood. Note that this will
403382 only ever return at most one element
404383
405384 :Args:
406- - png_img_path - a string corresponding to the path of a PNG image
407- - match_threshold - a double between 0 and 1 below which matches will
408- be rejected as element not found
385+ - img_path - a string corresponding to the path of a image
409386
410- :return: possibly-empty list of ImageElements
387+ :return: possibly-empty list of Elements
411388 """
412- els = []
413- try :
414- els .append (self .find_element_by_image (png_img_path , match_threshold ))
415- except NoSuchElementException :
416- pass
417- return els
389+ with open (img_path , 'rb' ) as i_file :
390+ b64_data = base64 .b64encode (i_file .read ()).decode ('UTF-8' )
391+
392+ return self .find_elements (by = By .IMAGE , value = b64_data )
418393
419394 def find_element_by_accessibility_id (self , accessibility_id ):
420395 """Finds an element by accessibility id.
0 commit comments