问题描述
我正在尝试使用python硒库对多个网站进行截图。
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()
我认为我应该使用browser.close(),但我不知道该怎么做。
I think I should use browser.close(), but I don't know exactly how.
推荐答案
您应该花一些时间阅读所使用的不同语句的文档。您错误地使用了几种。
You should spend some time reading the docs for the different statements that you are using. You are using several incorrectly.
我认为这会起作用。一个问题可能是,如果页面加载时间过长,将不允许浏览器使用 browser.get()
导航到新页面。您可以尝试发送ESC密钥或通过谷歌搜索找到的许多其他选项之一。
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()
这篇关于使用Selenium(python)拍摄多个URL的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!