0

(Raspbian Buster, Apache2, PHP 7, Python 3, Geckodriver 0.23)

I have a python script which is called by a php script with shell_exec(). When I run the python script from terminal while in the same location as the php script, it works like a charm. But when I access my apache server with localhost, the php works, but the python script does not run.

I did some researching and I found the php error log also gives my python errors. It says that I need to put my geckodriver in PATH (see below), which it is and also works when I run it within terminal but suddenly it does not when run from within php. It cannot be from any permission problems, I gave all the owner rights to the www-data group.

Error:

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

PHP code:

if ($_SESSION["loaded"] == False) {
    $output = shell_exec('python3 py/infoam.py');
    $_SESSION["loaded"] = True;
} elseif ($_SESSION["agendanr"] == 0 and $_SESSION["magisternr"] == 0) {
    $output = shell_exec('python3 py/infoam.py');
} elseif ($_SESSION["agendanr"] == 1 and $_SESSION["magisternr"] == 0) {
    $output = shell_exec('python3 py/infona.py');
} elseif ($_SESSION["agendanr"] == 0 and $_SESSION["magisternr"] == 1) {
    $output = shell_exec('python3 py/infonm.py');
} else {
    $output = shell_exec('python3 py/infonam.py');
}

The related python code:

from __future__ import print_function
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import requests
import time
import pickle
import os.path
headers = {"User-agent":"*my user agent*"}

class client:
    def __init__(self, usr, pwd):
        self.usr = usr
        self.pwd = pwd

    def magister(self):
        driver = webdriver.Firefox()
        driver.get("https://farel.magister.net/magister/#/vandaag")
        driver.maximize_window()
        driver.implicitly_wait(20)
        time.sleep(5)
        elem = driver.find_element_by_id("username")
        elem.send_keys(self.usr)
        elem.send_keys(u'\ue007')
        driver.maximize_window()
        driver.implicitly_wait(20)
        elem = driver.find_element_by_id("rswp_password")
        elem.send_keys(self.pwd)
        elem.send_keys(u'\ue007')
        driver.maximize_window()
        driver.implicitly_wait(10)
        time.sleep(5)
        elem = driver.find_element_by_class_name("last-grade").text
        lis = elem.split()
        grade, clas = lis[0], lis[1]
        driver.quit()
        return(grade + '\n' + clas + '\n')

    def write(self):
        file = '/var/www/html/txt/' + self.usr + '.txt'

    with open(file, 'w') as writefile:
            writefile.write(self.magister())

def read():
    f = open("/var/www/html/txt/acc.txt", "r")
    for x in f:
        keys = []
        for y in x.split(','):
            keys.append(y.strip())
        print(keys)
        person = client(keys[0], keys[1])
        person.write()

read()

Does anybody know why it throws this error?

Jelle
  • 198
  • 9
  • 2
    as I know Apache runs code as user `www-data` so it uses different settings, different enivronment, different folder, etc. and it may have different value in `PATH` so it may not know where is `geckodriver`. Maybe put `geckodriver in folder with python script or use Python script to display `PATH` and you will see in which folders it is searching `geckodriver. – furas Dec 19 '19 at 21:39
  • 1
    OR simply use `Firefox(executable_path=r'/full/path/to/geckodriver')` – furas Dec 19 '19 at 21:44
  • possible duplicate https://stackoverflow.com/questions/40208051/selenium-using-python-geckodriver-executable-needs-to-be-in-path – AidanGawronski Dec 20 '19 at 00:56
  • Have you tried running it from terminal with www-data user instead of sudo? – elMeroMero Dec 25 '19 at 20:21

0 Answers0