launchd failing to run python script with phantomJS - python

I have a python script that does some things online. Every 10 seconds launchd (OSX 10.11.3 El Capitan) launches a shell script, which calls the python script, which goes online using selenium webdriver.PhantomJS() (or should).
When running the shell script from the terminal, everything succeeds. When running in the configuration described above but with webdriver.Firefox() instead of webdriver.PhantomJS(), everything succeeds. However, when running through launchd -> shell -> python with webdriver.PhantomJS(), it exits with abnormal code: 1.
A simplified version of my code (with all the imports):
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.common.exceptions import NoSuchElementException
d = webdriver.PhantomJS()
d.close()
What am I missing? I would suspect full path names, but I do not explicitly import Firefox() either, and that does work...

Is it failing on an import failure? I had a similar-sounding problem, outlined here: Running Python Script with Launchd: imports not found
What was helpful: using LaunchControl, capturing Standard Error, and finding out python could not import a specific package. Not sure if that's your problem, but Standard Error will tell you what abnormal code: 1 actually is.

Related

Pycharm is running script headless successfully but terminal throwing error

Hello all,
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
dr = webdriver.Chrome(ChromeDriverManager().install(), options=options)
dr.get("url")
cWait = WebDriverWait(dr, 5)
usernameField = cWait.until(EC.presence_of_element_located((By.ID, "txtUserID")))
while running the above set of lines of code, when i click on pycharm run for this python file, it works in headless in good way but when I use
terminal and write code something like
python main.py
the console gives this output
====== WebDriver manager ======
Current google-chrome version is 92.0.4515
Get LATEST driver version for 92.0.4515
Driver [C:\Users\bc62700\.wdm\drivers\chromedriver\win32\92.0.4515.107\chromedriver.exe] found in cache
DevTools listening on ws://127.0.0.1:53298/devtools/browser/b967343c-b07a-43a3-8d1f-d8b41e62e50d
[0906/071237.616:INFO:CONSOLE(19)] "Uncaught TypeError: Cannot read property 'txtUserID' of undefined", source: URL (19)
It is working absolutely fine with pycharm run button, but not with terminal
please help as because of this reason I am not able to make this as schedular event.
When you run it from within pycharm, it is using a specific python, which may be different from your system's default python. Check the run configuration in pycharm and see what the path is to the python version it is using, and try to execute it using the full path to that python, i.e. instead of python main.py it would be something like /path/to/your/project/venv/bin/python /path/to/your/project/main.py
If that's not it, try giving us a more complete explanation of your situation, so that someone can try to reproduce what's happening and investigate further.

Execute Python Program line-by-line automatically

I'm doing a python script with selenium. To do tests I use command promp, this way i know if my line of command is working without needing to execute the program over and over to correct line by line. The thing is, i need a way to automatically start this lines so i don't need to keep retype it everytime i want to do some tests.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = webdriver.ChromeOptions()
options.add_argument('lang=pt-br')
driver = webdriver.Chrome(executable_path=r'./chromedriver.exe', options=options
From the command line, running python -m pdb <yourscript.py> should open the Python Debugger and allow you to step through. Each line is run by entering n and returning.

The following code is running through python IDE but not from CMD or by direct execution

import webbrowser
import pyautogui
import time
webbrowser.open("https://meet.google.com/huq-etkk-pwv",new=0,autoraise=True)
#auto_input
time.sleep(10)
pyautogui.hotkey('ctrl','d')
pyautogui.hotkey('ctrl','e')
time.sleep(2)
#custom coordinates are adviced
#coordinates work on standard chrome window with 1920*1080 screen size 16:9 ratio
pyautogui.click(972,428)
When i run this via CMD i get an error in the module pyautogui with Tk() name not defined and when i try to execute this directly it just opens a window of CMD and then stops and does nothing but works perfectly when i run it via the Python IDE.
Your code works fine for me on my Kubuntu (18.04.)
Which IDE do you use? Is it possible, that it by default runs a different version of python than your CMD (e.g. 3.7 in the IDE and 2.7 in the CMD)?
P.S.
would have posted it in a comment but lack the reputation

Timeout after launching IE using Selenium

I am trying to execute the code below. After launching the provided URL on the IE browser, the next line of code times out and does not get executed.
Please help!
Apps and version:
Selenium: 3.141
Python: 3.8
Pycharm IDE: 2020.1
Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.Ie(executable_path="...\IEDriverServer.exe")
driver.get("https://google.co.in") # Launch is successful.
print(7+7) # This is not executed. The process times out.
I am able to proceed now.
I was using 64bit IE-driver, I tried with 32-bit now which is allowing me to proceed after browser launch.
No time-out observed.
Thanks

Python import module only works in a new terminal window

I'm trying to run the following python script:
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('https://www.everlane.com/collections/mens-luxury-tees/products/mens-v-antique')
driver.save_screenshot('screen.png') # save a screenshot to disk
print driver.current_url
images = driver.find_elements_by_tag_name('img')
for image in images:
print image.get_attribute('src')
However, every time I try to run it, I get this error:
FitValet-MacBook-Pro:desktop fitvalet$ python selenium.py
Traceback (most recent call last):
File "selenium.py", line 1, in <module>
from selenium import webdriver
File "/Users/fitvalet/Desktop/selenium.py", line 1, in <module>
from selenium import webdriver
ImportError: cannot import name webdriver
FitValet-MacBook-Pro:desktop fitvalet$
But I've installed the module using pip install selenium and it installed fine. When I run a new terminal window, enter python, and then type in from selenium import webdriver, it imports fine. If I exit() python, and then re-enter and try again, the same above error happens, in that it can't import selenium. If I re-open terminal, then it works again, but only in the terminal python window. I can even type out every line of code and it prints the images fine in the terminal!
It never works if I just try to run the script on its own. Any ideas as to why this is? Thanks!!!
WOW. I can't believe this, but my little script, which I so simply named "selenium.py", was the problem. The answer is, DON'T DO THIS! When the script said from selenium import webdriver, it somehow thought it was calling itself, and creating major errors.
I renamed the script to "myselenium.py" and it worked fine.

Categories