Selenium (twenty): expected_conditions determine page elements

1. Analyzing element (expected_conditons)

As a python just go to the development of children, before developing only the method predecessors package read it again, learn while selenium basis. See the package method to determine what elements are present, determine whether the element is visible, but does not exist in the knowledge base, and a look ignorant force.

Until the time of writing the case of reptiles, suddenly I found a module in selenium attracted me, because I did not come across in other developing this module, so I went to understand a bit, then this will have a blog.

selenium is generally referred expected_conditions module EC, a series of collected scene determination method.

expected_condtions determination method provides 16 page elements.

We will use Baidu home page to demonstrate it (think of any page).

In front of the code are the same, so I EDITORIAL up.

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

base_url = "http://www.baidu.com"
driver = webdriver.Firefox()
driver.implicitly_wait ( . 5 )
 # implicit display waiting and waiting there, the timeout whichever larger 
driver.get (base_url)

title_is (): determine the title of the current page is fully equal to the expected string and returns a Boolean value

= WebDriverWait Demo (Driver, 10) .until (EC.title_is (U " Baidu, you know " ))
 Print ( " judge title, returns a Boolean value: " , Demo)

result:

 

 

title_contains (): determine whether the current page title contains the expected string and returns a Boolean value 

= WebDriverWait Demo (Driver, 10) .until (EC.title_contains (U " Baidu, " ))
 Print ( " Analyzing title, returns a Boolean value: " , Demo)

result:

 

 

presence_of_element_located (): determining whether an element is added dom tree, does not mean that the elements must be seen 

= WebDriverWait Demo (Driver, 10) .until (EC.presence_of_element_located ((By.ID, ' kW ' )))
 '' ' determines whether an element is added to the dom tree, it does not imply that a certain element is visible, if target returns WebElement '' ' 
Print (Demo)

result:

 

 

visibility_of_element_located (): determining whether an element is visible, the visible representative of the non-hidden elements, and the width and height of the elements is not 0 

= WebDriverWait Demo (Driver, 10) .until (EC.visibility_of_element_located ((By.ID, ' SU ' )))
 '' ' is determined whether an element is added to the inside and dom visible, and the visible representative of elements may display a wide and high greater than 0 '' ' 
Print (Demo)

result:

 

 

visibility_of (): determine whether the element is visible, if visible returns that element 

= WebDriverWait Demo (Driver, 10) .until (EC.visibility_of (driver.find_element (= By.ID by, value = ' kW ' )))
 '' ' Analyzing element is visible, if visible returns the element ' '' 
Print (Demo)

result:

 

 

presence_of_all_elements_located (): determining whether at least one element is present in dom tree, for example, if there are elements of the class n pages are 'coumn-md-3', name as long as there exists an element, the method returns True 

= WebDriverWait Demo (Driver, 10) .until (EC.presence_of_all_elements_located ((By.CSS_SELECTOR, ' .mnav ' )))
 '' ' is determined whether there is at least one element is present in dom tree, if the target returns a list ' '' 
Print (Demo)

result:

text_to_be_present_in_element_value (): determining the value attribute is included in an element of the expected string 

= WebDriverWait Demo (Driver, 10) .until (EC.text_to_be_present_in_element_value ((By.CSS_SELECTOR, ' #su ' ), U ' Baidu, ' ))
 '' ' determination attribute specifies whether the element contains the expected string , Boolean value '' ' 
Print (Demo)

result:

 

 

text_to_be_present_in_element (): determine text text contains an element of the expected string 

= WebDriverWait Demo (Driver, 10) .until (EC.text_to_be_present_in_element ((By.XPATH, " // * [@ ID =" U1 '] / A [. 8] " ), U ' set ' ))
 ' '' Analyzing whether the specified element contains the expected string, it returns a Boolean value '' ' 
Print (Demo)

result:

 

 

frame_to_be_availabe_and_switch_to_it (): determine whether the frame can switch into it, if you can, True is returned and switch into it, otherwise it returns False (not found an example)

invisibility_of_element_located (): determining whether an element is not present or is not visible in the tree dom

= WebDriverWait Demo (Driver, 10) .until (EC.invisibility_of_element_located ((By.CSS_SELECTOR, ' #swfEveryCookieWrap ' )))
 '' ' is determined whether an element exists in dom or not visible, if visible returns False, not visible this element returns ' '' 
# Note #swfEveryCookieWrap on this page is a hidden element 
Print (Demo)

result:

 

 

element_to_be_clickable (): an element is determined to see and are enable (active), the so called clickable 

= WebDriverWait Demo (Driver, 10) .until (EC.element_to_be_clickable ((By.XPATH, " // * [@ ID = 'U1'] / A [. 8] " ))). the Click ()
 '' ' Analyzing a whether the element is visible and is enable, the representative can click on '' ' 
Print (Demo)

result:

 

 

staleness_of (): an element such as removed from the dom tree, returns True or False (not found an example)

element_to_be_selected: to determine whether an element is selected, generally used for select the drop-down list (could not find an example)

 

element_selection_state_to_be: determining whether an element selected in line with expectations (could not find an example)

element_located_selection_state_to_be: the same way as with the above, but the above method to the incoming positioning element, this method pass locator (could not find an example)

alert_is_present: judgment on whether there is a page alert (could not find an example)

Guess you like

Origin www.cnblogs.com/liuhui0308/p/12080397.html