2

I am trying to make an internet radio based on opening and closing the web browser to start and stop the radio but I can't for some reason close the browser using different methods.

I have already tried using the subprocess method.

import subprocess as sp
web = sp.Popen("epiphany http://www.google.com", shell=True)

but after doing that i could neither close the process with

web.kill()

nor with

web.terminate()

It simply did nothing.

I also tried using the selenium method but as I am trying to do this on a raspberry pi, so the only browser is epiphany that is not supported and I could not get a different supported browser to install.

The webbrowser module in python does not seem to have any closing function

Is there a way I could manage to close the browser?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
mateuszdrwal
  • 91
  • 1
  • 8
  • 5
    Possible duplicate of [How to terminate a python subprocess launched with shell=True](http://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true) – taras Aug 21 '16 at 19:47
  • Why don't you use Selenium? – George Petrov Aug 21 '16 at 20:54
  • @GeorgePetrov as i said, i am doing this on a raspberry pi 3 and i couldnt get a browser to install so i have to use epiphany (lightweight browser for the pi) but it is not supported by selenium unless i got the concept all wrong – mateuszdrwal Aug 22 '16 at 15:05

2 Answers2

0

It seems like you want to close the browser that just opened. Assuming that you only have one epiphany instance open, you can use

sp.Popen("killall epiphany")

Which will kill all instance of epiphany. If you have multiple instances you will need to get the id of the running epiphany that you want to kill.

sp.Popen("epiphany http://www.google.com & export PID=$!", shell=True)
# Do Stuff Here With Epiphany
sp.Popen("kill -9 $PID")

This should open a new epiphany instance to www.google.com, and save PID into your environment variables. Then, you can get the value of PID using $PID and kill the browser process.

Brydenr
  • 798
  • 1
  • 19
  • 30
  • this for some reason did not work for me. both methods raise a child_exception with the reason "no such file or directory" i also noticed that if i do 'echo $PID' while epiphany is running with the second method it does not return anything, but im not sure if it should – mateuszdrwal Aug 23 '16 at 16:39
0

after doing alot of research i found out that i could kill the browser by typing

pkill epiphany

into the terminal so i just used this code to close it in python:

import os
os.system("pkill epiphany")
mateuszdrwal
  • 91
  • 1
  • 8