21
21
22
22
from selenium .webdriver .common .by import By
23
23
from 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
26
25
27
26
from selenium .webdriver .remote .command import Command as RemoteCommand
28
27
35
34
from .errorhandler import MobileErrorHandler
36
35
from .switch_to import MobileSwitchTo
37
36
from .webelement import WebElement as MobileWebElement
38
- from .imagelement import ImageElement
39
-
40
- DEFAULT_MATCH_THRESHOLD = 0.5
41
37
42
38
# From remote/webdriver.py
43
39
_W3C_CAPABILITY_NAMES = frozenset ([
@@ -207,6 +203,7 @@ def find_element(self, by=By.ID, value=None):
207
203
:rtype: WebElement
208
204
"""
209
205
# if self.w3c:
206
+
210
207
# if by == By.ID:
211
208
# by = By.CSS_SELECTOR
212
209
# value = '[id="%s"]' % value
@@ -218,8 +215,6 @@ def find_element(self, by=By.ID, value=None):
218
215
# elif by == By.NAME:
219
216
# by = By.CSS_SELECTOR
220
217
# value = '[name="%s"]' % value
221
- if by == By .IMAGE :
222
- return self .find_element_by_image (value )
223
218
224
219
return self .execute (RemoteCommand .FIND_ELEMENT , {
225
220
'using' : by ,
@@ -251,9 +246,6 @@ def find_elements(self, by=By.ID, value=None):
251
246
# Return empty list if driver returns null
252
247
# See https://github.com/SeleniumHQ/selenium/issues/4555
253
248
254
- if by == By .IMAGE :
255
- return self .find_elements_by_image (value )
256
-
257
249
return self .execute (RemoteCommand .FIND_ELEMENTS , {
258
250
'using' : by ,
259
251
'value' : value })['value' ] or []
@@ -370,51 +362,34 @@ def find_elements_by_android_viewtag(self, tag):
370
362
"""
371
363
return self .find_elements (by = By .ANDROID_VIEWTAG , value = tag )
372
364
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 ):
375
366
"""Finds a portion of a screenshot by an image.
376
367
Uses driver.find_image_occurrence under the hood.
377
368
378
369
: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
382
371
383
- :return: an ImageElement object
372
+ :return: an Element object
384
373
"""
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 ):
401
380
"""Finds a portion of a screenshot by an image.
402
381
Uses driver.find_image_occurrence under the hood. Note that this will
403
382
only ever return at most one element
404
383
405
384
: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
409
386
410
- :return: possibly-empty list of ImageElements
387
+ :return: possibly-empty list of Elements
411
388
"""
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 )
418
393
419
394
def find_element_by_accessibility_id (self , accessibility_id ):
420
395
"""Finds an element by accessibility id.
0 commit comments