php - 如何通过apache服务器执行php调用的带有selenium的python脚本?

标签 php python html apache selenium

我想通过本地 apache 服务器中的站点在 Python 中执行以下脚本:

#!/Python34/python
from selenium import webdriver 
driver=webdriver.Firefox()
driver.get("C:\wamp64\www\desenvol\index.html")
elem1 = driver.find_element_by_link_text("call another page")
elem1.click()

apache 配置正确,这是我使用 php 代码的页面:

<!doctype html>
<html>
<head>
<title>Light Controller</title>
</head>


<?php
if (isset($_POST['LightON']))
{
exec('python hello.py');
echo("on");
}
?>

<form method="post">
<button name="LightON">Light ON</button>&nbsp;
</form> 


</html>

最佳答案

提供 python 脚本的完整路径,即:

shell_exec('python /full/path/to/hello.py');

如果您想安全起见,还请提供 python 二进制文件的完整路径。

shell_exec('/usr/local/bin/python /full/path/to/hello.py');

要查找 python 二进制文件的完整路径,请打开 shell 并输入:

which python

  1. 确保 apache 用户具有 hello.py 的执行权限。
  2. 我在您的 html 上没有看到任何带有“调用另一个页面”文本的元素。

更新:

你也可以使用python的SimpleHTTPServer ,类似:

from BaseHTTPServer import BaseHTTPRequestHandler
import urlparse
class GetHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        parsed_path = urlparse.urlparse(self.path)
        self.send_response(200)
        self.end_headers()
        #self.wfile.write(message)
        if (parsed_path.query == "LightON"):
            from selenium import webdriver 
            driver=webdriver.Firefox()
            driver.get("http://stackoverflow.com")
            elem1 = driver.find_element_by_link_text("Questions")
            elem1.click()
            self.wfile.write("Command Executed")
        return

if __name__ == '__main__':
    from BaseHTTPServer import HTTPServer
    server = HTTPServer(('localhost', 8080), GetHandler)
    print 'Starting server, use <Ctrl-C> to stop'
    server.serve_forever()

上面的代码会在8080端口打开一个webserver,并等待LightON请求,收到后执行 Selenium 代码。

要激活它,只需创建一个指向它的链接,例如

<a href="http://localhost:8080/LightON"> LightON </a>

PS:我已经测试了代码,它按预期工作。

关于php - 如何通过apache服务器执行php调用的带有selenium的python脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37515412/

相关文章:

php - MySQL where 子句中的一个或多个参数

php - Laravel 5.3 $appends 不工作

javascript - 从 Angular JS 中的 URL 列表递归返回 JSON 对象

python - 如何在一个单元格内的 ipython 笔记本中循环更新两个子图

javascript - 元素之后的选择器 jquery

jquery - 每个 HTML5 占位符属性有两种不同的样式?

java - 使用 MySql 和 PHP 进行 Android 登录

php - 将新值保存到 Laravel session 数组

python - 大型阵列的优化平均?

python - 如何处理 Tornado 中的 MIME 类型?