我想从PHP调用python Selenium webdriver脚本,我不断收到以下错误:
“Traceback(最近一次调用最后一次):文件”seltest.py“,第12行,在setUp self.driver = webdriver.Firefox()文件”/usr/local/lib/python2.7/dist-packages/ selenium / webdriver / firefox / webdriver.py“,第59行,在 init self.binary,超时),文件”/usr/local/lib/python2.7/dist-packages/selenium/webdriver / firefox/extension_connection.py“,第47行,在 init self.binary.launch_browser(self.profile)文件”/usr/local/lib/python2.7/dist-packages/selenium/webdriver /firefox/firefox_binary.py“,第64行,在launch_browser self._wait_until_connectable()文件”/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py“,第103行,在_wait_until_connectable self._get_firefox_output())WebDriverException:消息:'在我们连接之前,浏览器似乎已经退出。输出为:None'“
从命令行运行时,python脚本运行正常。
test.php的内容:
<?php
$test = `python seltest.py 2>&1`;
var_dump ($test);
?>
seltest.py的内容:
#! /usr/bin/python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Seltext(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_seltext(self):
driver = self.driver
driver.get(self.base_url + "/?gws_rd=ssl")
driver.find_element_by_id("gbqfq").clear()
driver.find_element_by_id("gbqfq").send_keys("what is selenium webdriver")
driver.find_element_by_id("gbqfb").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
我已经准备好确认Web服务器的用户有权运行python脚本。我甚至试图以我的用户和组运行Web服务器,但仍然没有运气。
我在Ubuntu 12.04上使用Apache 2.2.22,PHP 5.3.10-1ubuntu3.14,Selenium 2.43.0和Python 2.7.3运行它。