Common Element Operations
After locating the element, you need to operate on the element, such as mouse click, keyboard operation, etc., depending on which operations the object we locate supports. In general, all operations that interact with the page will be through the WebElement interface
The commonly used methods of manipulating elements in webdriver are as follows:
1. clear(): Clear the contents of the object
driver.find_element(By.XPATH,'//input[@id="kw"]').clear()
2. send_keys(): Simulate key input on the object
driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')
3. click(): Click the object to simulate the user's click
driver.find_element(By.XPATH,'//a[text()="地图"]').click()
4. submit(): Submit the form, the required object must be a form
driver.find_element(By.ID,'form').submit()
5. size: returns the size of the object
driver.find_element(By.XPATH,'//input[@id="kw"]').size
6. text: Get the text of the object
driver.find_element(By.XPATH,'//a[text()="地图"]').text
7. get_attribute("attribute name"): Get the attribute value of the object
driver.find_element(By.XPATH,'//input[@id="kw"]').get_attribute('maxlength')
8. is_displayed(): Used to determine whether the object is visible, that is, whether the display attribute of css is none
driver.find_element(By.XPATH,'//input[@id="kw"]').is_displayed()
9. is_enabled(): Determine whether the object is disabled
driver.find_element(By.XPATH,'//input[@id="kw"]').is_enabled()
10. is_selected(): Determine whether the object is selected
driver.find_element(By.XPATH,'//input[@id="kw"]').is_selected()
11. tag_name: Get the object tag name
driver.find_element(By.XPATH,'//input[@id="kw"]').tag_name
12. location: Get element coordinates
driver.find_element(By.XPATH,'//input[@id="kw"]').location
13. screenshot('screenshot name'): screenshot
driver.find_element(By.XPATH,'//input[@id="su"]').screenshot('test_baidu.png')
code show as below:
Take Baidu homepage as an example
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
current_path = os.path.dirname(os.path.abspath(__file__)) # 当前路径
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe') # driver路径
driver = webdriver.Chrome(executable_path=driver_path) # Firefox,Ie等
driver.get('https://www.baidu.com/') # 打开网站
# send_keys() 模拟按键输入值
driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')
time.sleep(2)
# clear() 清除对象的内容
driver.find_element(By.XPATH,'//input[@id="kw"]').clear()
time.sleep(2)
# click() 模拟用户点击
driver.find_element(By.XPATH,'//a[text()="地图"]').click()
# text 获取对象的文本
element_a = driver.find_element(By.XPATH,'//a[@href="http://tieba.baidu.com/"]')
print(element_a.text)
# 截图
driver.find_element(By.XPATH,'//input[@id="su"]').screenshot('test_baidu.png')
element_obj = driver.find_element(By.XPATH,'//input[@id="kw"]')
print(element_obj.size) # size 获取元素的尺寸
print(element_obj.get_attribute('maxlength')) # get_attribute("属性名") 获取指定属性的值
print(element_obj.is_displayed()) # is_displayed() 元素是否显示
print(element_obj.is_enabled()) # is_enabled() 元素是否可用
print(element_obj.is_selected()) # is_selected() 复选框是否被选中
print(element_obj.tag_name) # tag_name 获取元素的标签名
print(element_obj.location) # location 获取元素左上角的坐标
Finally: In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】
Software Testing Interview Documentation
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.