So I've been trying to write a short script that automates pulling up the Unity3D help pages (five of them), as well as Unity itself and TortoiseHG. Since the webbrowser module was not behaving the way I wanted (on my Win7 box), I've been using subprocess.run.
Last night, I thought I finally fixed the thing and got it doing what I want, only to have it open five windows instead of tabs if Firefox wasn't already running. Same issue I was having with webbrowser.open, go figure.
But that's not why I'm here. I'm here because when I opened the script with IDLE to try to fix that problem, I ran into a new problem: the script will run just fine if I double click on it, but if I try to run it through IDLE with F5 I get
Traceback (most recent call last):
File "C:\Users\<me>\AppData\Local\Programs\Python\Python35-32\GameDevEnvironment.py", line 5, in <module>
subprocess.run(r'start firefox -new-tab https://unity3d.com/learn/tutorials', shell=True)
AttributeError: 'module' object has no attribute 'run'
My full code:
import os
import subprocess
#import time
subprocess.run(r'start firefox -new-tab https://unity3d.com/learn/tutorials', shell=True)
#time.sleep(5)
subprocess.run(r'start firefox -new-tab https://answers.unity3d.com', shell=True)
subprocess.run(r'start firefox -new-tab https://unity3d.com/learn/tutorials/topics/scripting', shell=True)
subprocess.run(r'start firefox -new-tab https://docs.unity3d.com/Manual/index.html', shell=True)
subprocess.run(r'start firefox -new-tab https://docs.unity3d.com/ScriptReference/index.html', shell=True)
#os.startfile(r'C:\Program Files\Unity\Editor\Unity.exe')
#os.startfile(r'C:\Program Files\TortoiseHg\thgw.exe')
I was going to try using the time.sleep method to get a Firefox process running so that it wouldn't open a new window for each subprocess.run call (as said before, they'll open in tabs if Firefox is already running). I commented it out while trying to solve the new problem.
What I've already tried: looking for a .pyc file that could be confusing import. Found none. Making sure none of my methods or classes are named after Python modules--as you can see, there aren't any method or class definitions, and unless Python has a module called GameDevEnvironment.py... I found lots of questions here and elsewhere of people having the opposite issue of running in IDLE not on double click etc., but couldn't find anything obviously relevant...
I appreciate your time and help!
Since the original question is a two-parter, here is a two part answer (both are also in the comments).
The reason I could not run the script from IDLE was that the shell version was older than the code I wrote. Basically, in Python 3.2 the Subprocess class didn't exist. When I uninstalled Python32 to ensure I could only open in Python35, the problem was solved.
The other problem, which led to this problem, was subprocess.run() opening several Firefox windows if there was not already an instance of Firefox running. The solution was to add a time.sleep() in between the first and second calls to subprocess.run(). For me, eight seconds was a good amount of time for the process to get going and allow the first page to partially load, which made the second page load faster. Since my machine is old, I ended up putting a sleep in between each tab--it looks a lot smoother, but it adds twenty seconds to the process; not very elegant.
If you landed here looking for a way to get results without getting Selenium, my advice: get Selenium. I'm pretty sure it would have saved me time and frustration had I just downloaded and learned it.
The finished code (had to break the links because my account is new):
import os
import subprocess
import time
os.startfile(r'C:\Program Files\Mozilla Thunderbird\thunderbird.exe')
subprocess.run(r'start firefox -new-tab unity3d[breaking link]/learn/tutorials', shell=True)
time.sleep(8)
subprocess.run(r'start firefox -new-tab answers.unity3d[breaking link]', shell=True)
time.sleep(3)
subprocess.run(r'start firefox -new-tab unity3d[breaking link]/learn/tutorials/topics/scripting', shell=True)
time.sleep(3)
subprocess.run(r'start firefox -new-tab docs.unity3d[breaking link]/Manual/index.html', shell=True)
time.sleep(3)
subprocess.run(r'start firefox -new-tab docs.unity3d.[breaking link]/ScriptReference/index.html', shell=True)
time.sleep(3)
os.startfile(r'C:\Program Files\TortoiseHg\thgw.exe')
os.startfile(r'C:\Program Files\Unity\Editor\Unity.exe')
Related
I'm trying to open a python script from another without blocking using Popen. The starter script opens the driver script, and (using a tkinter button) the driver script can open a side script. When I try to call Popen in the driver script, nothing happens - the shell opens very briefly then closes. However, when I run the driver script without calling the starter first (as __main__), the side script opens fine.
I've posted the code below, am I missing something regarding nested Popen calls? For the executable, I've tried sys.executable, python.exe, and pythonw.exe. None work. Threading might work as an alternative to Popen, but the side script uses tkinter as well, so I would have to find a method for making tkinter thread safe.
starter.py:
pid = subprocess.Popen(["pythonw.exe", "driver.py"])
driver.py:
pid = subprocess.Popen([sys.executable, "side_script.py"]
I found the issue, thankfully it was a simple one. Per Charles Duffy's advice, I tried looking at any errors using output, error = pid.communicate(), and saw there was an error finding the file. I adjusted the code to point to the file using sys.path[0], and it solved the problem.
I am writing a script by using selenium. My problem is when the chrome has been automatically updated, my script is not working. So, my solution is learning the web chrome version (not driver) at the beginning and run the related chrome driver. So on my desktop I will keep all versions and run the correct one. But I could not find a solution to get the version of chrome. I will kindly appreciate the helps! Thanks in advance!
Try this.
import subprocess
output = subprocess.check_output(
r'wmic datafile where name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" get Version /value',
shell=True
)
print(output.decode('utf-8').strip())
output
Version=79.0.3945.117
I made a PyQt program which runs Chrome web driver via Selenium.
It works normally in Mac or Linux, but it prints a weird line in console when I run this program on Windows, as below.
KLIB_SelfTest return : KLR_OK
The log itself is not a big deal, but when it comes with Pyinstaller, it produces some error when compiled by --noconsole option. When I remove that option it works fine, but I want the console window not to be showed.
Moreover, I'm really curious about which part of the Chrome driver produces that log. I searched almost every information as far as I can, but I couldn't find anything related with the log.
Thanks in advance for your help.
I have used Selenium in the past and was pretty happy with it.
When I started a new project I wanted to use it again. Since it has been a long time and my ChromeDriver 2.20 didn't seem to work I updated to 2.25. This seemed to work but that's only an illusion.
It seems the driver.get("whateveryouputhere") never fully loads the page.
from selenium import webdriver
driver = webdriver.Chrome('/Users/Sascha-mac/Desktop/chromedriver')
driver.get("http://www.python.org")
driver.close()
This does open a Chrome tab that navigates to www.python.org as you'd expect it to. However, it doesn't close the driver.
Even a simple print "HELLO" doesn't come through until I manually close Chrome.
I have updated to the newest version of Selenium, ChromeDriver. I work on MacOS Sierra with Python 2.7. I have also tested with different websites - no difference.
Where did I go wrong?
driver.close() closes the window.
driver.quit() terminates the browser.
macOS apps that have multiple documents generally do not terminate when the last window is closed. The macOS convention is that only single window apps should do that and traditionally few of those do that.
You might be confused by the common Windows behavior.
So typically I am too stubborn to use forum sites myself (call me proud), but this has been driving me absolutely mad.
My goal is simply to start Chrome on a server machine, through that server's python WSGI script. I am aware of the bug that prevents Python from using Popen under WSGI, but I've figured out what seemed like a workable (albeit somewhat hack-y) solution: instead of calling Popen with the actual command, I created a .bat and called the bat through Popen with the shell argument set to True. This actually works for just about everything other than what I needed it to; i.e., calling start iexplore worked, start firefox worked, even notepad and mspaint, but the only thing that didn't work, was calling start chrome. I've even tried a few command-line arguments, like start chrome --single-process, but to no avial.
It's like Windows just hates Chrome. I would greatly appreciate any help, as I've been pulling my hair out over this for the last 48 hours.
tl;dr: Chrome won't start from a batch script called using subprocess.Popen, but everything else will.
Relevant Technical Stuff
Python script [excerpt]:
import subprocess
command="<path>\\start-browser.bat" # Absolute path to Batch file
subprocess.Popen(command,shell=True)
Batch file:
start /d "C:\Documents and Settings\<Me>\Local Settings\Application Data\Google\Chrome\Application" chrome.exe
System:
Windows XP sp3, 32-bit
Python 2.7
Apache 2.2
I haven't tried this in your case, but I know a lot of Chrome's oddities come about because it is installed per user. Have you tried logging in as Administrator and installing Chrome for all users?