使用带有phantomjs的EC.text_to_be_present_in_element的TimeoutException

时间:2017-10-01 17:39:34

标签: python python-2.7 selenium phantomjs

我使用从https://phantomjs.org下载的Python 2.7.13,Selenium 3.3.3版和PhantomJS 2.1.1。

如果我在Fedora 25(或Windows 7)上运行以下脚本,它可以在Firefox上正常运行,但可以使用PhantomJS提升selenium.common.exceptions.TimeoutException

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException
import unittest2

FF = True
#FF = False

class Test(unittest2.TestCase):

    @classmethod
    def setUpClass(self):
        if FF:
            binary = FirefoxBinary('/usr/bin/firefox')
            d = DesiredCapabilities.FIREFOX
            self.driver = webdriver.Firefox(firefox_binary=binary)
        else:
            self.driver = webdriver.PhantomJS("phantomjs")
            self.driver.maximize_window()

        self.base_url = "http://phantomjs.org/"
        self.verificationErrors = []
        self.accept_next_alert = True
        self.result = 0

    def test01(self):
        driver = self.driver
        driver.get(self.base_url)

        print("Check http://phantomjs.org/ title")
        WebDriverWait(driver, 10).until(
            EC.text_to_be_present_in_element((By.XPATH, "/html/head/title"), "PhantomJS | PhantomJS")
        )

    @classmethod
    def tearDownClass(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest2.main(failfast=True)

使用Firefox:

$ python test.py
Check http://phantomjs.org/ title
.
----------------------------------------------------------------------
Ran 1 test in 9.433s

OK

使用PhantomJS:

$ python test.py
Check http://phantomjs.org/ title
E
======================================================================
ERROR: test01 (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 36, in test01
    EC.text_to_be_present_in_element((By.XPATH, "/html/head/title"), "PhantomJS | PhantomJS")
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 


----------------------------------------------------------------------
Ran 1 test in 21.169s

FAILED (errors=1)

1 个答案:

答案 0 :(得分:0)

我通过更改

修复了它
EC.text_to_be_present_in_element((By.XPATH, "/html/head/title"), "PhantomJS | PhantomJS")

EC.title_contains("PhantomJS | PhantomJS")

这是to @ImpulseTheFox

中列出的库调用之一