问题
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.
回答1:
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()
来源:https://stackoverflow.com/questions/44926240/take-screenshot-of-multiple-urls-using-selenium-python