TimeoutException 使用 EC.text_to_be_present_in_element 和 phantomjs

TimeoutException using EC.text_to_be_present_in_element with phantomjs

我正在使用 Python 2.7.13、Selenium 版本 3.3.3 和从 https://phantomjs.org 下载的 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)

我通过更改

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

EC.title_contains("PhantomJS | PhantomJS")

这是 docs

中列出的库调用之一