UI自动化:环境检测/基本操作/元素、xpath、css定位/常见操作/上传文件/获取文本信息与属性的值/判断元素是否展示/多窗口的操作
01 环境检测:
from selenium import webdriver
import time
d = webdriver.Chrome() # 打开谷歌浏览器
d.get('https://www.baidu.com') # get() 在当前窗口打开网址
time.sleep(3) # 休眠3秒钟
d.quit() # 关闭掉谷歌浏览器,并且退出驱动
02 基本操作:
from selenium import webdriver
import time
# 你是怎么操作的,你的代码就怎么写
dr = webdriver.Chrome()
dr.maximize_window() # 最大化窗口
dr.implicitly_wait(5)# 隐式等待,在页面元素的查找过程中,最大等待5秒钟
# 打开网址
dr.get('https://www.baidu.com')
time.sleep(5)
# 关闭浏览器,并且退出驱动
dr.quit()
03 元素定位:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# 初始化一个浏览器,最大化窗口,设置等待时间
d = webdriver.Chrome()
d.maximize_window()
d.implicitly_wait(5)
d.get('http://qq.imsam.cn/')
# 滚动条的下拉 js语句: window.scrollTo(0,与顶部的距离); px(像素)
src = "window.scrollTo(0,1000);" # 1000px
d.execute_script(src)
time.sleep(3)
# 想要去操作页面上的任何元素,都需要先定位到该元素
# 定位 需要导入By 这个包 from selenium.webdriver.common.by import By
# click() 点击定位到的元素
# d.find_element(By.LINK_TEXT,'登录').click() # 根据完整文本链接进行定位
d.find_element(By.PARTIAL_LINK_TEXT,'登').click() # 根据部分的文本链接进行定位
time.sleep(1)
# 根据标签名 进行定位 TAG_NAME 不推荐使用,重复率太高了,selenium定位原则,找到第一个就不找了
# send_keys() 输入内容
# d.find_element(By.TAG_NAME,'input').send_keys('admin')
# 根据属性定位,能够直接使用的属性 id,name,class
# 使用name 定位到用户名
d.find_element(By.NAME,'log').send_keys('admin')
time.sleep(1)
# 使用id定位到密码
d.find_element(By.ID,'user_pass').send_keys('admin123')
time.sleep(1)
# 使用class定位到登录按钮 class="button button-primary button-large"
# 在html代码中,一个属性可以有多个值 ,每个值之间用 空格隔开
d.find_element(By.CLASS_NAME,'button-primary').click()
time.sleep(3)
d.quit()
04 xpath定位:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
d = webdriver.Chrome()
d.implicitly_wait(10)
d.maximize_window()
d.get('http://qq.imsam.cn/wp-login.php')
time.sleep(1)
d.find_element(By.XPATH,'//input[@class="input"]').send_keys('admin')
time.sleep(1)
d.find_element(By.XPATH,'//input[@type="password"]').send_keys('admin123')
time.sleep(1)
d.find_element(By.XPATH,'//input[@class="button button-primary button-large"]').click()
time.sleep(3)
d.quit()
05 css定位:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
d = webdriver.Chrome()
d.implicitly_wait(10)
d.maximize_window()
d.get('http://qq.imsam.cn/wp-login.php')
time.sleep(1)
d.find_element(By.CSS_SELECTOR,'[name="log"]').send_keys('admin')
time.sleep(1)
# d.find_element(By.CSS_SELECTOR,'[id="user_pass"]').send_keys('admin123')
d.find_element(By.CSS_SELECTOR,'#user_pass').send_keys('admin123')
time.sleep(1)
# d.find_element(By.CSS_SELECTOR,'[class="button button-primary button-large"]').click()
d.find_element(By.CSS_SELECTOR,'.button-primary').click()
time.sleep(3)
d.quit()
06 常见操作:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
d = webdriver.Chrome()
d.implicitly_wait(10) # 隐式等待
d.maximize_window() # 最大化窗口
time.sleep(1)
d.get('http://qq.imsam.cn/') # get 在当前窗口打开一个网址
time.sleep(2)
print(d.title) # 获取浏览器的标题
print(d.current_url) # 获取当前的网址
print(d.page_source) # 获取当前页面的源代码
time.sleep(1)
d.find_element(By.CSS_SELECTOR,'[name="s"]').send_keys('青春')
time.sleep(3)
d.find_element(By.CSS_SELECTOR,'[name="s"]').clear() # 清空输入的内容
time.sleep(3)
d.quit()
07 上传文件:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
d = webdriver.Chrome()
d.implicitly_wait(10) # 隐式等待
d.maximize_window() # 最大化窗口
time.sleep(1)
d.get('https://www.baidu.com/')
# 点击 相机 图标
d.find_element(By.CSS_SELECTOR,'[class="soutu-btn"]').click()
time.sleep(1)
# 只针对于 input标签的 上传按钮
# 定位到上传按钮,然后直接 send_keys() 输入文件的路径
d.find_element(By.CSS_SELECTOR,'[class="upload-pic"]').send_keys(R'/Users/sam/Downloads/taobao.png')
#R'/Users/sam/Downloads/taobao.png' 。这个是我们要上传的图片/文件 的路径
time.sleep(5)
d.quit()
08 获取文本信息与属性的值:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
d = webdriver.Chrome()
d.implicitly_wait(10) # 隐式等待
d.maximize_window() # 最大化窗口
time.sleep(1)
d.get('https://www.baidu.com/')
# 定位到设置 , text 获取元素的文本信息
msg = d.find_element(By.CSS_SELECTOR,'[id="s-usersetting-top"]').text
print(msg)
# 定位到 百度一下 按钮,获取属性对应的值 get_attribute('属性名称')
msg2 = d.find_element(By.CSS_SELECTOR,'[id="su"]').get_attribute('value') # 获取 value属性 对应的值
print(msg2)
d.quit()
09 判断元素是否展示:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
d = webdriver.Chrome()
d.implicitly_wait(10) # 隐式等待
d.maximize_window() # 最大化窗口
time.sleep(1)
d.get('https://www.baidu.com/')
# 定位到元素后, is_displayed() 如果显示,则返回True,不显示返回False
msg = d.find_element(By.CSS_SELECTOR,'[id="su"]').is_displayed()
print(msg)
d.quit()
10 多窗口的操作:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
d = webdriver.Chrome()
d.implicitly_wait(10) # 隐式等待
d.maximize_window() # 最大化窗口
time.sleep(1)
d.get('https://qiye.163.com/')
time.sleep(1)
# 定位到登录按钮
d.find_element(By.CSS_SELECTOR,'[class="btn-login"]').click() # 点击后,生成了一个新窗口
time.sleep(1)
# 获取所有的窗口 window_handles ,以列表list的形式 返回
all_window = d.window_handles # [第一个窗口,第二个窗口]
# switch_to.window() 切换窗口
d.switch_to.window(all_window[1])
time.sleep(1)
d.find_element(By.CSS_SELECTOR,'[id="accname"]').send_keys('admin@qq.com')
time.sleep(5)
d.quit()

浙公网安备 33010602011771号