Selenium2+python Automation 36-Judging the existence of elements

Source: Internet
Author: User
Tags xpath

Objective

Recently a lot of small partners are asking how to determine whether an element exists, this method is not in the selenium, need to write their own.

element does not exist, the operation element will be error, or the element has multiple, not the only time will be error. This article introduces two methods of judging the existence of elements.

First, find_elements method judgment

1.find_elements method is to find all the same properties on the page method, this method is actually very useful, can master the skills of the few, small series this time to play its effect

2. Because there are many methods of positioning the elements, so when the location method is not uniform and more trouble, here I choose CSS Positioning (have like XPath students can use the XPath syntax)

3. Write a function to determine, find the return ture, not found will return false (or more than one)

Second, Baidu input box as an example

1. Determine if an element with an ID of KW exists

2. Determine if the label is an INPUT element exists

3. Determine if the id is xxx element exists

Iii. methods of catching exceptions

1. If no element is found, throw an exception, return false

2. Return Ture If the element is found

3. However, this method has a disadvantage, if there are more than one element on the page, it will also return ture (that is, as long as there are elements on the page to return ture, no matter how many)

Iv. Reference Code

# Coding:utf-8
From selenium import Webdriver

Driver = Webdriver. Firefox ()
Driver.implicitly_wait (10)
Driver.get ("http://www.baidu.com")
def is_element_exist (CSS):
s = Driver.find_elements_by_css_selector (CSS_SELECTOR=CSS)
If Len (s) = = 0:
Print "element not found:%s"%css
Return False
Elif len (s) = = 1:
Return True
Else
Print "found%s elements:%s"% (len (s), CSS)
Return False

# Determine if there are no elements on the page with the ID of KW
If Is_element_exist ("#kw"):
driver.find_element_by_id ("kw"). Send_keys ("Yoyoketang")
# Determine if the page has an INPUT element that is not labeled
If Is_element_exist ("Input"):
Driver.find_element_by_tag_name ("Input"). Send_keys ("Yoyoketang")
# Determine if the page has an element with no ID of XXX
If is_element_exist ("xxx"):
driver.find_element_by_id ("xxx"). Send_keys ("Yoyoketang")

def iselementexist (CSS):
Try
Driver.find_element_by_css_selector (CSS)
Return True
Except
Return False

Print iselementexist ("#xxx")

Selenium2+python Automation 36-Judging the existence of elements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.