find_element(定位方式,值)
单个元素
find_elements(定位方式,值)
多个元素
1.用id定位 ??
2.用name定位
3.用class定位
4.用tag定位
5.用link定位
6.用partial link定位
7.用CSS定位
借助谷歌驱动函数包
webdriver
借助pycharm-Python语言
import unittest
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
?
class TestElement(unittest.TestCase):
????@classmethod
????def setUpClass(self):
????????self.driver = webdriver.Chrome()
????????self.driver.maximize_window()#最大窗口
???????
?# 设置等待时间
self.driver.implicitly_wait(30)
????# 定位元素--id
????def test_01_id(self):
????????#打开页面
????????self.driver.get('http://www.baidu.com)
????????# id定位元素
????????uernameInput = self.driver.find_element(By.ID,'username')
????????uernameInput.clear()
????????uernameInput.send_keys('wangliuliu')
????????# 密码
????????passwordInput = self.driver.find_element(By.ID,'password')
????????passwordInput.clear()
????????passwordInput.send_keys('123456')
????# 定位元素-name
????def test_02_name(self):
????????# 打开网页...不打开新的网页 在原来的基础上进行操作
????????# self.driver.get('http:www.baidu.com')
????????# name定位
????????rePasswordInput = self.driver.find_element(By.NAME,'rePassword')
????????rePasswordInput.clear()
????????rePasswordInput.send_keys('654321')
????# 定位元素class
????def test_03_class(self):
????????em = self.driver.find_element(By.CLASS_NAME,'mtext')
????????em.clear()
????????em.send_keys('805256908@qq.com')
????# 标签定位
????def test_04_tag(self):
????????# 根据标签 查找多个
????????list = self.driver.find_elements(By.TAG_NAME,'input')
????????# get获取_attribute属性()
????????print('value属性值:',list[4].get_attribute('value'))
????# 超链接定位
????def test_05_link(self):
????????denglu = self.driver.find_element(By.LINK_TEXT,'登录')
????????denglu.click()
??# 回到注册
????????self.driver.find_element(By.PARTIAL_LINK_TEXT,'立即注').click()
????# css定位
????def test_06_css(self):
????????dateInput = self.driver.find_element(By.CSS_SELECTOR,'#registerForm > table > tbody > tr:nth-child(9) > td > input')
????????dateInput.clear()
????????dateInput.send_keys('2021-08-09')
????# xpath...//标签名[@属性1=属性值1 and @属性2=属性值2]
????def test_07_xpath(self):
????????a = self.driver.find_element(By.XPATH,'//input[@id="username"]')
????????a.clear()
????????a.send_keys('t37uijnfe9')
????????b = self.driver.find_element(By.XPATH,'//*[@id="password"]')
????????b.clear()
????????b.send_keys('123456')
????@classmethod
????def tearDownClass(self):
????????sleep(3)
????????self.driver.quit()
if __name__ == '__main__':
????unittest.main()