Skip to content

Commit 0785d10

Browse files
committed
adding __hash__ method to webelement so that one can put found elements in a python set for uniqueness
Fixes Issue #7011
1 parent 3056803 commit 0785d10

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

py/selenium/webdriver/remote/webelement.py

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515

1616
"""WebElement implementation."""
17+
import hashlib
1718
import os
1819
import zipfile
1920
try:
@@ -382,6 +383,9 @@ def find_elements(self, by=By.ID, value=None):
382383
return self._execute(Command.FIND_CHILD_ELEMENTS,
383384
{"using": by, "value": value})['value']
384385

386+
def __hash__(self):
387+
return int(hashlib.md5(self._id.encode('utf-8')).hexdigest(), 16)
388+
385389
def _upload(self, filename):
386390
fp = StringIO()
387391
zipped = zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED)

py/test/selenium/webdriver/common/element_equality_tests.py

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ def testDifferentElementsAreNotEqual(self):
3737

3838
self.assertNotEqual(body, div)
3939

40+
def testSameElementsFoundDifferentWaysShouldNotBeDuplicatedInASet(self):
41+
self._loadSimplePage()
42+
body = self.driver.find_element(By.TAG_NAME, "body")
43+
xbody = self.driver.find_elements(By.XPATH, "//body")
44+
s = set(xbody)
45+
s.add(body)
46+
self.assertEqual(1, len(s))
4047

4148
def _pageURL(self, name):
4249
return "http://localhost:%d/%s.html" % (self.webserver.port, name)

0 commit comments

Comments
 (0)