Web UI Automated Testing - positioning element (b)

This article finishing the basics of positioning elements - the more elements targeting methods .

A plurality of positioning elements Approaches

  Targeting the same as a single element, a plurality of positioning elements and there are also eight ways corresponding thereto, i.e., id, name, class_name, tag_name, link_text, partial_link_text, xpath and css_selector. The difference is the method name element to be written in the plural form. The results obtained are typically a plurality of positioning elements is a list type, specific elements can be accessed by an index.

Second, examples

  Home to Baidu, for example, by find_elements_by_tag_name demonstration mode. As shown, a total of 15 tag called input element, wherein a eighth Baidu search box, a ninth 'Baidu,' the search button, the corresponding subscripts 7 and 8, respectively.

figure 1

 1 import time
 2 
 3 from selenium import webdriver
 4 
 5 # 创建driver实例
 6 driver = webdriver.Chrome()
 7 # 打开百度首页
 8 driver.get('https://www.baidu.com')
 9 # 通过tag_name属性找出百度首页中的所有input元素
10 inputs = driver.find_elements_by_tag_name('input')
11 # 打印inputs类型
12 print(type(inputs))
13 # 打印inputs长度
14 Print (len (Inputs))
 15  # traverse the inputs, printing each element in the list 
16  for I in Range (len (Inputs)):
 . 17      Print ( ' {0}: {}. 1 ' .format (I, Inputs [ I]))
 18 is  # search in Baidu enter input search box 
. 19 inputs [. 7] .send_keys ( ' input ' )
 20 is inputs [. 8 ] .click ()
 21 is  # wait 2 seconds 
22 is the time.sleep (2 )
 23 is  # exit the driver and close the browser 
24- driver.quit ()
View Code

 

Guess you like

Origin www.cnblogs.com/cnblogs0/p/11229222.html