0
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()

Now, how can I take multiple screenshots and save them in the different filename? As you can see I am trying to save the filename according to domain URL but failed.

See the error below:

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

Can anyone please tell me what is the exact problem is? Will be a big help.

2 Answers 2

2

Try to move driver.close() out of loop:

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()

Also note that x is already a string, so there is no need in str(x)

P.S. I'm not sure that http://stackoverflow.com.png filename is acceptable, you might need to use:

filename = x.split('//')[-1] + '.png'
Sign up to request clarification or add additional context in comments.

13 Comments

WOW, WOW, and just WOW. Now I can get both png files. Fantastic Job.
But some questions I have, What is the usage of [-1] in the filename? And will ask you second after this. :)
x.split('//')[-1] means split site-name, e.g. 'http://google.com' by double slash (the result is list ['http:', 'google.com']) and select element with index [-1] (the last element - 'google.com')
Okay Cool. Thanks for it, Andersson. You just remove a ton of tension.
See this question for other ways to convert a URL to a valid filename.
|
2

Tried to run command without establishing a connection

you are closing the browser within your for loop... so the 2nd time through the loop it fails with the error above (since the browser is closed, the connection to geckodriver has already been terminated).

other issues:

  • you are setting the page_load_timeout after you have already fetched the page, so it is not doing anything useful.
  • using CHROMEDRIVER_PATH as the name for Geckodriver is just confusing. Chromedriver is not used at all here.

2 Comments

Oh, I am so sorry about it. I did use a chrome driver but I forgot to change the name of the variable.
Hi, I have posted a new question. why not give it a try to see stackoverflow.com/questions/52082123/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.