我想通过本地 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>
</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
- 确保 apache 用户具有
hello.py
的执行权限。 - 我在您的 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/