使用Selenium(python)拍摄多个URL的屏幕截图

编程入门 行业动态 更新时间:2024-10-28 07:34:41
本文介绍了使用Selenium(python)拍摄多个URL的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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', 'youtube'... ]

如果网站加载时间太长,我希望程序开始打开下一个网站。

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(''+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('' + data[index]) except: print(data[index] + ' took too long') else: # where images saved browser.save_screenshot('/.../' + str(index) + '.png') browser.quit()

更多推荐

使用Selenium(python)拍摄多个URL的屏幕截图

本文发布于:2023-11-23 11:24:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621273.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   截图   屏幕   Selenium   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!