Calling a Selenium Python Script from PHP
I am tying to call a python Selenium webdriver script from PHP and I keep getting the following error:
"Traceback (most recent call last): File "seltest.py", line 12, in setUp self.driver = webdriver.Firefox() File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 59, in init self.binary, timeout), File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in init self.binary.launch_browser(self.profile) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 64, in launch_browser self._wait_until_connectable() File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 103, in _wait_until_connectable self._get_firefox_output()) WebDriverException: Message: 'The browser appears to have exited before we could connect. The output was: None'"
The python script works just fine when running it from the command line.
Content of test.php:
<?php
$test = `python seltest.py 2>&1`;
var_dump ($test);
?>
Content of 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()
I have all ready confirmed that the user for the web server has permission to run the python script. I have even tried to run the web server as my user and group and still no luck.
I'm running this on Ubuntu 12.04 with Apache 2.2.22, PHP 5.3.10-1ubuntu3.14, Selenium 2.43.0, and Python 2.7.3.
链接地址: http://www.djcxy.com/p/62750.html