0

I want compare 2 images.But I am getting below error.

fp.seek(0)
AttributeError: 'WebElement' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

   Traceback (most recent call last):
     File "D:\......\test_ImagePreview.py", line
 107, in test_PlaylistExport
       imCompare2 = Image.open(driver.find_element_by_xpath("xhtml:html/xhtml:body/xhtml:img"))
  File "C:\Python34\lib\site-packages\PIL\Image.py", line 2254, in open
    fp = io.BytesIO(fp.read())
AttributeError: 'WebElement' object has no attribute 'read'

1st image is stored in my computer and second image is from the webpage.

Script I'm using:

imCompare1 = Image_Path
imCompare2 = Image.open(driver.find_element_by_xpath("xhtml:html/xhtml:body/xhtml:img"))

Comparediff = ImageChops.difference(imCompare1, imCompare2).getbbox()
print (Comparediff)

from DOM:< xhtml:img src="https://XXX.XX.XXX.XXX/Path1/Path2/Files.aspx?method=getPreview&fileid=5959&session=hddkdcgywtdw025u4ikiljk0" alt="ttps://XXX.XX.XXX.XXX/Path1/Path2/Files.aspx?method=getPreview&fileid=5959&session=hddkdcgywtdw025u4ikiljk0/ >

Can someone tell me what Am I doing wrong? As its in Xhtml. How I can compare images from webpage?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Tester P
  • 167
  • 1
  • 5
  • 15
  • I guess you can't open web element found by xpath- this is not an image file! – Andersson Dec 04 '15 at 10:41
  • You can make a screenshot of the hole page: `driver.save_screenshot('name_your_file')` – Andersson Dec 04 '15 at 10:44
  • @Andersson: with screenshot entire page would be considered right?? not only image. – Tester P Dec 04 '15 at 10:49
  • Yes, entire page... but , as I know, for some browsers (like Edge) you can use `screenshot` method to get only image element screenshot... you need to have up-to-date `selenium` version also – Andersson Dec 04 '15 at 12:30

1 Answers1

0

Finally I am able to complete this by downloading image to my system with below code.

img_url = "https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"

fileN = open('Imgfile.png','wb')
fileN .write(urllib.request.urlopen(img_url).read())
fileN .close()

Then comparing both the stored images

Tester P
  • 167
  • 1
  • 5
  • 15