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 Answers2

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'
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • WOW, WOW, and just WOW. Now I can get both png files. Fantastic Job. – Utkarsh Agrawal Aug 06 '18 at 13:36
  • But some questions I have, What is the usage of [-1] in the filename? And will ask you second after this. :) – Utkarsh Agrawal Aug 06 '18 at 13:38
  • `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'`) – Andersson Aug 06 '18 at 13:40
  • Okay Cool. Thanks for it, Andersson. You just remove a ton of tension. – Utkarsh Agrawal Aug 06 '18 at 13:53
  • See [this question](https://stackoverflow.com/questions/27253530/save-url-as-a-file-name-in-python) for other ways to convert a URL to a valid filename. – JeffC Aug 06 '18 at 15:09
  • Andersson, I know that this question should now be closed but can you please tell me that I want to make a link in the terminal so that I can see the image by just click on that link. So Is there any-way? – Utkarsh Agrawal Aug 06 '18 at 15:17
  • Do you mean that you want your script to print into terminal output results as clickable links or something like that? – Andersson Aug 06 '18 at 15:19
  • Yeah! Something like that. :) – Utkarsh Agrawal Aug 06 '18 at 15:25
  • So for now your output is something like `stackoverflow.com.png`, but you want to see some clickable strings that can open the image file from your local folder? I'm not sure that this is kinda simple task actually :) It depends on OS, how exactly you want those "links" to looks like, third-party libs... – Andersson Aug 06 '18 at 15:38
  • You can run local server from the folder where images will be downloaded with `python -m SimpleHTTPServer 8080` and then you can access them via browser by openeing `localhost:8080` - all files should be available as links – Andersson Aug 06 '18 at 15:44
  • Hi @Andersson, I have posted another question. Would you like to give a try to see it? Here is the https://stackoverflow.com/questions/48970906/how-to-write-for-loop-content-in-html-code-python – Utkarsh Agrawal Aug 08 '18 at 11:46
  • Hi, Andersson, I have posted another new question please see the below question. – Utkarsh Agrawal Aug 15 '18 at 05:52
  • Here is the full url. https://stackoverflow.com/questions/50484845/adding-new-keys-and-value-to-the-older-dictionary-without-changing-the-keys-of-o Looking for you. Thanks – Utkarsh Agrawal Aug 15 '18 at 05:53
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.
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143