GECKODRIVER_PATH = 'F:/geckodriver.exe'
firefox_options = Options()
firefox_options .add_argument("-headless")
driver = webdriver.Firefox(executable_path=CHROMEDRIVER_PATH, firefox_options = firefox_options )
test = []
test.append('http://google.com')
test.append('http://stackoverflow.com')
for x in test:
print x
driver.get(x)
driver.set_page_load_timeout(20)
filename = str(x)+'.png'
driver.save_screenshot( filename )
driver.close()
现在,如何拍摄多个屏幕截图并将其保存在其他文件名中?如您所见,我正在尝试根据域URL保存文件名,但失败了。
看到以下错误:
http://google.com
http://card.com
Traceback (most recent call last):
File "F:\AutoRecon-master\test.py", line 125, in <module>
driver.get(x)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 326, in get
self.execute(Command.GET, {'url': url})
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Tried to run command without establishing a connection
谁能告诉我确切的问题是什么?将有很大帮助。
答案 0 :(得分:2)
尝试将driver.close()
移出循环:
for x in test:
print x
driver.get(x)
driver.set_page_load_timeout(20)
filename = str(x)+'.png'
driver.save_screenshot( filename )
driver.close()
还要注意,x
已经是一个字符串,因此str(x)
中不需要
P.S。我不确定http://stackoverflow.com.png
文件名是否可以接受,您可能需要使用:
filename = x.split('//')[-1] + '.png'
答案 1 :(得分:2)
试图在不建立连接的情况下运行命令
您正在关闭for循环中的浏览器...因此循环中第二次失败,并显示上述错误(由于浏览器已关闭,因此geckodriver的连接已终止)。
其他问题: