0

I have the following Python code to take screenshots of webpages. It is working good for most cases, but when i tried to print

http://www.totalwine.com/wine/red-wine/pinot-noir/c/000018

i am getting a different screenshot - different from the actual page (I am getting the correct screenshot sometimes). Can someone help. I've attached the output screenshot that i got as well. Please load the above link on a browser and you will see a different page.

I'm thinking the possible reasons could be 1) Timing of page load 2) Popup

Can someone help

def screenshot_util(url):
    browser = webdriver.PhantomJS(service_log_path='ghostdriver.log')
    browser.set_window_size(1024, 768)    
    browser.get(url)
    browser.save_screenshot('temp.png')
    print(browser.current_url)
    browser.quit()
    return

url_to_print = 'http://www.totalwine.com/wine/red-wine/pinot-noir/c/000018'
screenshot_util(url_to_print)

Screenshot of the response from code

tindu edward
  • 299
  • 1
  • 3
  • 17

2 Answers2

0

Programmatically click the yes button on the pop-up and wait a few seconds like this:

from selenium import webdriver
from time import sleep

def screenshot_util(url):
    browser = webdriver.PhantomJS(service_log_path='ghostdriver.log')
    browser.set_window_size(1024, 768)
    browser.get(url)
    browser.find_element_by_id("btnYes").click()
    sleep(4)
    browser.save_screenshot('temp.png')
    print(browser.current_url)
    browser.quit()
    return

url_to_print = 'http://www.totalwine.com/wine/red-wine/pinot-noir/c/000018'
screenshot_util(url_to_print)

For clarity the lines I added to your code were:

from time import sleep
...
    browser.find_element_by_id("btnYes").click()
    sleep(4)
Dan-Dev
  • 8,957
  • 3
  • 38
  • 55
  • Actually, my crawler is very generic. Clicking this button makes it very specific to this page. Upon more analysis i am thinking it might not be because of the popup. It might be because of timing because i am getting the correct screenshot sometimes – tindu edward Aug 14 '17 at 11:52
0

Your code was nearly perfect but had a small bug. You are trying to take a screenshot and save it simply as temp.png. Here webdriver gets the name of the screenshot but is not sure about the location where you want to save the screenshot.

browser.save_screenshot('temp.png')

Solution:

As a solution I have used your own code and have provided a logical path to the Screenshots sub-directory (./Screenshots/) already created under my project space. driver seems to be happily saving the screenshot there named as temp.png. Here is the modified code block:

from selenium import webdriver

def screenshot_util(url):
    browser = webdriver.PhantomJS(service_log_path='./Logs/logs.log', executable_path="C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe")
    browser.set_window_size(1024, 768)
    browser.get(url)
    browser.save_screenshot('./Screenshots/temp.png')
    print(browser.current_url)
    browser.quit()
    return

url_to_print = 'http://www.totalwine.com/wine/red-wine/pinot-noir/c/000018'
screenshot_util(url_to_print)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352