0

I used some code for several months, where I open a browser with a specific webpage. Through this webpage I can access the frontend of a database, where I can filter some data and export them to a .txt file. After closing the browser, my python code will continue, working with the data, which were exported before.

Today I had the problem, that the browser was opened and the python code didn't wait until the browser was closed, but instantly ran on and did nothing, because the file (where the exported data will be saved) was still empty.

Until now I open the browser with (URL is a replacement):

browser = r'C:\Program Files\Internet Explorer\iexplore.exe'
url = r'https://www.google.com' 
command = r'{browser} "{url}"'.format(browser=browser, url=url)
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()

After I searched for this error on some webpages, I changed it from subprocess.Popen to subprocess.run:

process = subprocess.run(command, capture_output=True, text=True)
output = process.stdout
error = process.stderr
returncode = process.returncode

But this didn't worked either.

Then by chance I noticed that another browser session was running in the background. As I closed this session, the code was running like before, stopping and waiting until the browser was closed. As soon as another session of the browser was already running, the code didn't work anymore and continued until the end.

I have now helped myself by inserting an input("Press Enter to continue...") after the subprocess call to be sure that the code will stop the execution until the file with the data is written, but this should be a temporary solution at most.

I did not find anything about this strange behavior. Did I something wrong? How can I handle this problem?

OsTeNg24
  • 274
  • 1
  • 15
Matze
  • 3
  • 1
  • Hi, what were you expecting to be written? Is it OK for your subprocess code to not be blocked? – rajah9 Oct 21 '20 at 11:41
  • I expected, that the program waited until the subprocess finished (the browser was closed). As I wrote, this exactly the case if I only have one browser session active, but not if there are more. The code should work the same way, no matter how many session are active.The description for subprocess.run is "Run the command described by args. Wait for command to complete, then return the returncode attribute." - so it should wait until closed. – Matze Oct 22 '20 at 08:00

1 Answers1

1

The problem is on the Internet Explorer side. IE (and I believe generally any browser) will notice that a browser process is already running, and the subprocess will finish as soon as it dispatches the event to the running browser to visit the URL you wanted to open.

The switch to subprocess.run() is correct as such, but not sufficient. Maybe find a way to block if iexplorer.exe is already running, or run a different (and by any reasonable metric better) browser instead when that happens.

As an aside, assembling the command into a string is both unnecessary and potentially problematic here. The difference is not that great on Windows, but you should still understand it.

process = subprocess.run([browser, url], capture_output=True, text=True)
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you very much, @tripleee for the clarification. I tested other browser as well (Chrome and Firefox) but encountered the same error. I used IE because it is the least used and the chances that another instcance is already running, was the lowest.The tipp to use another browser if an IE instance is already running is really good. Why could the use of a string here as args be problematic? The subprocess documentation says "The arguments used to launch the process. This may be a list or a string." – Matze Oct 23 '20 at 12:23
  • Because you blindly interpolate the URL into a quoted string without checking whether the URL contains a literal quote character, or something else which needs to be escaped. If you don't have a shell you don't need to escape anything from it, and you are doing a job which `subprocess` does anyway, only better. – tripleee Oct 23 '20 at 12:27
  • Ok, I understood. But in this case the URL is static, I know them, there can't be surpises... ;) – Matze Nov 05 '20 at 10:16