0

I am trying to take a screenshot of multiple websites using python selenium library.

Here I have an array of website like

data = array of website [ 'google.com', 'youtube.com'... ]

If a website takes too long to load, I want the program starts open next websites.

But this code doesn't do what I expected...

browser = webdriver.Chrome('/Users/wk/Desktop/checkSafeContent/chromedriver')

for index, url in enumerate(data):
    browser.set_page_load_timeout(30)    
    try:
        browser.get('http://'+data[index])
    except:
        print("takes too long")
        browser.quit()
        browser = webdriver.Chrome('/Users/wk/Desktop/checkSafeContent/chromedriver')
    else:
        browser.maximize_window()
        browser.implicitly_wait(20)
        # where images saved
        browser.save_screenshot('/.../'+str(index)+'.png')

browser.quit()

I think I should use browser.close(), but I don't know exactly how.

Sean
  • 489
  • 1
  • 8
  • 29
  • 1
    How is the behavior different from what you expected? – Guy Jul 05 '17 at 12:29
  • And you should post running code. Currently you have `else` without `if`. – Guy Jul 05 '17 at 12:30
  • https://docs.python.org/2/tutorial/errors.html. This website says that we can have else for try block. The codes work fine if browser = webdriver.Chrome('/Users/wk/Desktop/checkSafeContent/chromedriver') correctly points to chromedriver. – Sean Jul 05 '17 at 12:44
  • You shouldn't have to `quit()` the browser, just point it to the next URL and continue. `implicitly_wait` doesn't do what you think it does. You should read the docs and understand how it works and what it does. – JeffC Jul 05 '17 at 13:17

1 Answers1

0

You should spend some time reading the docs for the different statements that you are using. You are using several incorrectly.

I think this will work. One issue may be that if the page loads long, the browser will not be allowed to navigate to a new page with browser.get(). You might try sending an ESC key or one of the many other options you can find by googling.

I added the site to the "took too long" message so you would know which ones didn't finish loading in time.

browser = webdriver.Chrome('/Users/wk/Desktop/checkSafeContent/chromedriver')
browser.set_page_load_timeout(30)    
browser.maximize_window()

for index, url in enumerate(data):
    try:
        browser.get('http://' + data[index])
    except:
        print(data[index] + ' took too long')
    else:
        # where images saved
        browser.save_screenshot('/.../' + str(index) + '.png')

browser.quit()
JeffC
  • 22,180
  • 5
  • 32
  • 55