Python Selenium webdriver抛出属性错误

时间:2015-09-16 15:10:13

标签: python python-2.7 selenium selenium-webdriver

我在我们的框架中使用python selenium webdriver,最近我们一直看到如下错误

(<type 'exceptions.AttributeError'>, 
AttributeError("'unicode' object has no   attribute 'text'",)
,<traceback     object at 0x00000000037D8D48>) 

而且我们在产品中看到了这个错误,其中包含unicode charecters。事情总是发生在15次。

堆栈跟踪:

self.compare_units_test('Singapore')
    line 391, in compare_units_test
    self.assertTrue(home_page.is_loaded(), Errors.HOMEPAGE_LOGO_ERROR)
    File "", line 32, in is_loaded
    "Logo did not load results in 10 seconds")
    File "\venv\lib\site-packages\pscore\core\support\ps_wait.py", line 20,in      until_visible
    self._wait_until_visible(locator, timeout, message, True)
    File "venv\lib\site-packages\pscore\core\support\ps_wait.py", line 15, in  _wait_until_visible
    wait.until(EC.visibility_of_element_located(locator), message=message)
   File "\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 66, in until
    value = method(self._driver)
  File "\venv\lib\site- packages\selenium\webdriver\support\expected_conditions.py", line 72, in  __call__
    return _element_if_visible(_find_element(driver, self.locator))
   File \venv\lib\site- packages\selenium\webdriver\support\expected_conditions.py", line 90, in  _element_if_visible
    return element if element.is_displayed() else False
    AttributeError: 'unicode' object has no attribute 'is_displayed'

    2015-09-10 15:56:36 - INFO  wd_testcase.py:111 in run : Test Runner: Tearing down test: test_compare_units (tests.test.TestFlightsSearch)
2015-09-10 15:56:36 - INFO  wd_testcase.py:48 in tearDown : Test Runner: Attempting to teardown.
2015-09-10 15:56:36 - ERROR  wd_testcase.py:125 in run : Traceback (most recent call last):
  File \venv\lib\site-packages\pscore\core\wd_testcase.py", line 112, in run
    self.tearDown()
  File "\venv\lib\site-packages\pscore\core\wd_testcase.py", line 49, in tearDown
    WebDriverFinalizer.finalize(self.driver, self.has_failed(), self.logger, self.test_context)
  File "\venv\lib\site-packages\pscore\core\finalizers.py", line 28, in finalize
    WebDriverFinalizer.finalize_skygrid(driver, test_failed, test_context)    File "\lib\site-packages\pscore\core\finalizers.py", line 152, in finalize_skygrid
    WebDriverFinalizer.finalise_skygrid_driver_failure(driver, test_context)
    File "\venv\lib\site-packages\pscore\core\finalizers.py", line 168, in    finalise_skygrid_driver_failure
    final_url = driver.current_url
      File "venv\lib\site-   packages\selenium\webdriver\support\event_firing_webdriver.py", line 201, in    __getattr__
    raise AttributeError(name)AttributeError: current_url 

我们使用2.45版本的selenium和2.7.7版本的python。

当我挖到webdriver源代码时发现了这个

try:
str = basestring
except NameError:
pass

专门解决了webdriver遇到的unicode问题。 可能导致这种情况的任何想法?非常感谢帮助

1 个答案:

答案 0 :(得分:0)

找到问题的根本原因,看起来其中一个测试产生了一个新的浏览器窗口,后续测试使用对话框窗口而不是浏览器窗口来执行selenium命令。在某个时刻,其中一个测试执行了driver.quit,当后续测试试图访问driver.something时,由于驱动程序已被杀死,因此失败了。