程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python automated testing selenium (II) element location and operation

編輯:Python

One 、 location
The old version is used
driver.find_element_by_xxx()
The new version is

from selenium.webdriver.common.by import By
driver.find_element(By.XXX, “selector”)
If you match more than one , Returns the first match .
If it doesn't match , Throw out NoSuchElementException abnormal ( Report errors ).

What is mainly written here is the new version

1、 By element id Attribute to locate the element

ele = driver.find_element(By.ID, "IamID") # The type is WebElement class 

2、 By element name Attribute to locate the element


driver.find_element(By.NAME, "first").send_keys(" By element name Attribute to locate the element ")

3、 By element class Attribute to locate the element

driver.find_element(By.CLASS_NAME, 'poem').send_keys(" By element class Attribute to locate the element ")

4、 Locate the element by the tag name of the tag

print (driver.find_element(By.TAG_NAME, 'form').text)

5、 according to Hyperlink label To locate the element... Based on the text content in the

print (driver.find_element(By.LINK_TEXT, ' A round in the sky ,').text)

6、 according to Hyperlink label Medium Part of the text content to locate the element

print (driver.find_element(By.PARTIAL_LINK_TEXT, " Round ").text)

7、 according to css Selector to locate the element

print (driver.find_element(By.CSS_SELECTOR, '#tianshang').text)

8、 according to xpath Expression to locate the element

print (driver.find_element(By.XPATH, '//*[@id="tianshang"]').text)

Example

Two 、 operation
1、send_keys function : Parameters are the characters to be entered

driver.find_element(By.CSS_SELECTOR, '#tianshang').send_key("admin")

2、clear function : Empty data , No arguments

driver.find_element(By.CSS_SELECTOR, '#tianshang').clear()

3、click function : Click on the element , No arguments

driver.find_element(By.CSS_SELECTOR, '#tianshang').click()

4、set_window_size() Set the size of the browser
5、back() Control the browser back
6、forward() Control browser progress
7、refresh() Refresh current page
8、submit() For submitting forms
9、get_attribute(name) Get element attribute value
10、is_displayed() Set whether the element is visible to the user
11、size Returns the size of the element
12、text Get the text of the element


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved