Skip to content

Commit

Permalink
adding __hash__ method to webelement so that one can put found elemen…
Browse files Browse the repository at this point in the history
…ts in a python set for uniqueness

Fixes Issue #7011
  • Loading branch information
lukeis committed Feb 22, 2014
1 parent 3056803 commit 0785d10
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions py/selenium/webdriver/remote/webelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


"""WebElement implementation."""
import hashlib
import os
import zipfile
try:
Expand Down Expand Up @@ -382,6 +383,9 @@ def find_elements(self, by=By.ID, value=None):
return self._execute(Command.FIND_CHILD_ELEMENTS,
{"using": by, "value": value})['value']

def __hash__(self):
return int(hashlib.md5(self._id.encode('utf-8')).hexdigest(), 16)

def _upload(self, filename):
fp = StringIO()
zipped = zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED)
Expand Down
7 changes: 7 additions & 0 deletions py/test/selenium/webdriver/common/element_equality_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def testDifferentElementsAreNotEqual(self):

self.assertNotEqual(body, div)

def testSameElementsFoundDifferentWaysShouldNotBeDuplicatedInASet(self):
self._loadSimplePage()
body = self.driver.find_element(By.TAG_NAME, "body")
xbody = self.driver.find_elements(By.XPATH, "//body")
s = set(xbody)
s.add(body)
self.assertEqual(1, len(s))

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

0 comments on commit 0785d10

Please sign in to comment.