在UI自动化测试中,web类中提供了很多可以调用的方法,比如进行具体的清空操作、获取文本框中的输入信息等。具体如下:

1、clear():清空

 1 from selenium import webdriver
 2 from selenium.webdriver.common.by import By
 3 driver=webdriver.Chrome()
 4 driver.get("http://www.baidu.com")
 5 so=driver.find_element(By.ID,"kw")
 6 so.send_keys("admin")
 7 t.sleep(2)
 8 #对在输入框输入的文本进行清空操作
 9 so.clear() 
10 t.sleep(2)
11 driver.quit()

2、get_attribute():获取元素属性的值

 1 from selenium import webdriver
 2 from selenium.webdriver.common.by import By
 3 import time as t
 4 driver=webdriver.Chrome()
 5 driver.get("http://www.baidu.com")
 6 so=driver.find_element(By.ID,"kw")
 7 so.send_keys("selenium")
 8 t.sleep(2)
 9 #获取输入框输入元素的值
10 print(so.get_attribute("value"))  
11 t.sleep(2)
12 driver.quit()

3、get_attribute()实战

 1 from selenium import webdriver
 2 from selenium.webdriver.common.by import By
 3 import time as t
 4 driver=webdriver.Chrome()
 5 #窗口最大化
 6 driver.maximize_window()   
 7 driver.get("http://www.lagou.com")
 8 t.sleep(5)
 9 driver.find_element(By.LINK_TEXT,"登录").click()
10 t.sleep(3)
11 #定位到输入框
12 login=driver.find_element(By.NAME,"phone-account") 
13 t.sleep(2)
14 #获取输入框的placeholder的内容
15 print(login.get_attribute("placeholder"))
16 t.sleep(2)
17 driver.quit()

4、is_enabled():是否可以编辑

 1 from selenium import webdriver
 2 from selenium.webdriver.common.by import By
 3 import time as t
 4 driver=webdriver.Chrome()
 5 driver.maximize_window()
 6 driver.get("http://www.baidu.com")
 7 so=driver.find_element(By.ID,"kw")
 8 #检查定位到的输入框是否可以编辑
 9 print(so.is_enabled())   
10 driver.quit()

5、is_selected():是否勾选

 1 from selenium import webdriver
 2 from selenium.webdriver.common.by import By
 3 import time as t
 4 driver=webdriver.Chrome()
 5 driver.get("https://mail.sina.com.cn/")
 6 isLogin=driver.find_element(By.ID,"store1")
 7 print(isLogin.is_selected())
 8 t.sleep(2)
 9 isLogin.click()
10 print(isLogin.is_selected())
11 t.sleep(2)
12 driver.quit()

6、is_display():是否隐藏

1 from selenium import webdriver
2 from selenium.webdriver.common.by import By
3 driver=webdriver.Chrome()
4 driver.get("https://www.baidu.com/")
5 aboutBaidu=driver.find_element(By.XPATH,'//*[@id="bottom_layer"]/div/p[1]/a')
6 print("关于百度是否可见:",aboutBaidu.is_displayed())
7 t.sleep(3)
8 driver.quit()