AttributeError:“元组”对象没有属性“ is_enabled”

时间:2019-09-18 14:49:44

标签: python selenium

硒: 3.141.0,语言: Python 3。

访问下面的显式等待方法时出现以下错误。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait


driver = webdriver.Chrome(
    "Path\\ChromeDriver_32.exe")
driver.get('foo')

element = WebDriverWait(driver, 10).until(
    EC.staleness_of((By.XPATH, "//div[@class='loading_icon']")))

print(element)

预期结果:元素从DOM退出后返回True。

实际结果: AttributeError:“元组”对象没有属性“ is_enabled”

Traceback (most recent call last):
  File "......../temp.py", line 14, in <module>
    EC.staleness_of((By.XPATH, "//div[@class='loading_icon']")))
  File "...\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
    value = method(self._driver)
  File "...\Python\Python37-32\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 315, in __call__
    self.element.is_enabled()
AttributeError: 'tuple' object has no attribute 'is_enabled'

有人可以帮我做错什么吗?

1 个答案:

答案 0 :(得分:2)

您正在将元组传递给需要元素的方法。来自expected_conditions.staleness_of() documentation

  

class selenium.webdriver.support.expected_conditions.staleness_of(element)

     

等待直到元素不再附加到DOM。 element是等待的元素。

这与其他一些expected_conditions便捷方法不同,其他便捷方法带有 locator 参数。此特定方法不能接受定位符,因为定位符只能找到仍附加到DOM 的元素。

在分离元素之前先找到它:

from selenium.common.exceptions import NoSuchElementException

try:
    element = driver.find_element_by_xpath("//div[@class='loading_icon']")
    WebDriverWait(driver, 10).until(EC.staleness_of(element))
except NoSuchElementException:
    element = None