1

I'm making an application using Python to parse a webpage. For reference, this is the program's code:

import csv 
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.info('first log')

listsof = []
driver = webdriver.Firefox()
logging.info('Setup driver')
driver.implicitly_wait(30)
logging.info('next step')
driver.get("https://www.lacrossealerts.com/login")
logging.info('Got webpage')
username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
login = driver.find_element_by_name('login')
logging.info('Got elements')
username.send_keys("username")
password.send_keys("password")
password.send_keys(Keys.RETURN)
logging.info('Hit enter')
test = driver.find_elements_by_class_name('row-val')
contents = driver.page_source
test1 = driver.find_elements_by_class_name('row-val')
test2 = driver.find_element_by_class_name('timestamp')
logging.info('Found elements')
listsof.append(test2.text)
for e in test1:
    print(e.text)
    listsof.append(e.text)
print(listsof)
logging.info('Step before write to CSV')
with open('C:/Users/MyUser/Downloads/weather.csv', 'a', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    writer.writerow(listsof)
driver.close()
logging.info('Wrote to CSV')

In this, I had some logging. I get this:

INFO:root:first log
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:65058/hub/session {"sessionId": null, "desiredCapabilities": {"platform": "ANY", "javascriptEnabled": true, "version": "", "browserName": "firefox"}}
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
INFO:root:Setup driver
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:65058/hub/session/5cdbbf60-3a9f-4eca-96d2-7a026c47b56d/timeouts/implicit_wait {"sessionId": "5cdbbf60-3a9f-4eca-96d2-7a026c47b56d", "ms": 30000.0}
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
INFO:root:next step
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:65058/hub/session/5cdbbf60-3a9f-4eca-96d2-7a026c47b56d/url {"url": "https://www.lacrossealerts.com/login", "sessionId": "5cdbbf60-3a9f-4eca-96d2-7a026c47b56d"}
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
INFO:root:Got webpage
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:65058/hub/session/5cdbbf60-3a9f-4eca-96d2-7a026c47b56d/element {"using": "name", "sessionId": "5cdbbf60-3a9f-4eca-96d2-7a026c47b56d", "value": "username"}

So it looks like it doesn't get past one of these lines:

username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
login = driver.find_element_by_name('login')

Could this be a network or internet problem, that it doesn't have the proper permissions? Is there something else? When I run this in a batch file:

cd C:\Users\User\Downloads
python weather.py

It works perfectly. I'm running this as a scheduled task, on my user account. When it is set to Run only when this user is logged on, it works perfectly. However, when I try to run it on the System user account or Run whether this user is logged on or not, it doesn't complete the task. I would like for the task (cmd and launches Firefox) to not be displayed to the user, as to not disturb their work, since it is running every 20 minutes, but this isn't working. Is there a problem with the way I'm doing it? Is there a better way to "hide" this from the user?

8
  • 1
    Anything in the system log about the task? And you may need to add some logging code to your script to see where it fails.
    – jwalker
    Dec 26, 2013 at 23:44
  • @jwalker Didn't find anything in the log, it just records the task as started, but no errors or anything. And what would I log, and how, since it's a hidden task?
    – hichris123
    Dec 27, 2013 at 0:22
  • You can log to a file or to the system log. Just add logging code after every few lines and then narrow the failing block to see what exactly is wrong.
    – jwalker
    Dec 27, 2013 at 0:31
  • I've never used Selenium, so I may be of little help now. Use try-except and see what exception is occurring there. Also it looks strange to me that those requests are POST, and that find_element_by_name() makes a POST also.
    – jwalker
    Dec 27, 2013 at 1:07
  • @jwalker I'm not sure what to do for try-except, since I'm not sure what exception to occur. Any thoughts?
    – hichris123
    Dec 27, 2013 at 1:16

1 Answer 1

2

I know this is an old posting; but, just for anyone else that is struggling with this, the best approach to take is to utilize PhantomJS as the browser. Since its headless, it quietly runs the browser steps and processes. Also, it has no issues if run as part of a Python script via windows scheduled task while not logged in. All you need to do is: swap out 'driver = webdriver.Firefox()' with 'driver = webdriver.PhantomJS()' in your Python code, download the exe from here: http://phantomjs.org/ and place it in your path system environment variable, where you likely already have your Python dependencies path listed.

Not the answer you're looking for? Browse other questions tagged .