从PHP调用Selenium Python脚本

我打算从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:消息:'浏览器似乎有退出之前,我们可以连接。输出是:没有'“

从命令行运行它时,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。

链接地址: http://www.djcxy.com/p/62749.html

上一篇: Calling a Selenium Python Script from PHP

下一篇: Selenium: FirefoxProfile exception Can't load the profile