# 7. 判断元素是否存在




### is_element_present

判断元素是否存在。所有的容器类(BasePage, Container和Logical Container)都有```is_element_present```方法，如果对应名称的元素在页面上存在，该方法返回true。

```python
from selenium.webdriver.common.by import By
from webium import BasePage, Find

class PresencePage(BasePage):
    class_info = Find(by=By.CSS_SELECTOR, value='a[href*="/newclass"]')
    faq = Find(by=By.CSS_SELECTOR, value='a[href*="/faq"]')

    def __init__(self):
        super(PresencePage, self).__init__(url='http://itest.info')


if __name__ == '__main__':
    page = PresencePage()
    page.open()
    print('This should be True: ' + str(page.is_element_present('class_info')))
    print('This should be False: ' + str(page.is_element_present('no_such_link')))

```

注意：如果是通过Finds去定位的一组元素，我们也可以调用```is_element_present```方法来判断这组元素的存在性。如果这一组元素全部存在，返回True,如果有任意1个元素不存在，返回False。

### just_in_dom

默认情况下，```is_element_prasent```方法只有在这个元素可见的时候返回True。如果我们需要元素存在于dom中就返回True的话，那么设置```just_in_dom=True```


### timeout

timeout是等待```is_element_prasent```返回True的时间，单位是秒。

```python
print('This should be True: ' + str(page.is_element_present('class_info', just_in_dom=True, timeout=3)))
```



原始封面

![课程图片](https://images.unsplash.com/photo-1501556466850-7c9fa1fccb4c?w=300)


