Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

Running Python Selenium Script through PHP

I am having a PHP script which executes a python script containing Selenium.

$command = escapeshellcmd("/home/clouduser/bots/telegram/send_alerts");
$output = shell_exec($command); 
echo $output; 

Selenium works just fine when running it outside of php, I only get the following error when the python script is executed through php.

I have set folder permissions to 777 as well as group to www-data.

Traceback (most recent call last):
  File "/home/clouduser/bots/telegram/send_alerts/main.py", line 35, in <module>
    selenium()
  File "/home/clouduser/bots/telegram/send_alerts/main.py", line 27, in selenium
    driver = webdriver.Chrome(executable_path='/home/clouduser/bots/telegram/send_alerts/chromedriver', options=options) 
  File "/home/clouduser/bots/telegram/send_alerts/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/home/clouduser/bots/telegram/send_alerts/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 98, in start
    self.assert_process_still_running()
  File "/home/clouduser/bots/telegram/send_alerts/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 109, in assert_process_still_running
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1

The code I am using is:

#!/home/clouduser/bots/telegram/send_alerts/bin/python3.8 
from selenium import webdriver
import time

def selenium():
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument("--window-size=1920,1080")
    options.add_argument("--start-maximized")
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(executable_path='/home/clouduser/bots/telegram/send_alerts/chromedriver', options=options) 
    driver.get ("https://google.com") 
    time.sleep(5)
    print(driver.title)
    driver.quit()
    return selenium
 
selenium()

As mentioned running the python file alone works perfectly.

I appreciate any help, thank you!

question from:https://stackoverflow.com/questions/65831963/running-python-selenium-script-through-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...