IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> selenium自动化测试简单准备 -> 正文阅读

[开发测试]selenium自动化测试简单准备

selenium自动化测试简单准备

下载驱动器

http://chromedriver.storage.googleapis.com/index.html

下载与谷歌版本相同或最近版本。

chrome版本查看

帮助中查看
在这里插入图片描述

导包

from selenium import webdriver

创建浏览器对象(以chrome为例)

driverpath = r' '# 驱动器路径(chromedriver.exe)
driver = webdriver.Chrome(executable_path=driverpath)

demo

import time
from selenium import webdriver

url = 'https://www.baidu.com'
driverPath = r'' # 驱动器路径
driver = webdriver.Chrome(executable_path=driverPath)
driver.get(url)
driver.maximize_window() # 最大化页面
time.sleep(3)
driver.quit()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mAD8Kk8G-1641714144495)(C:\Users\29273\AppData\Roaming\Typora\typora-user-images\image-20220109142353225.png)]

基本函数

尺寸,位置

driver.minimize_window()  #最大化页面
driver.minimize_window()  #最小化页面
driver.set_window_size(200, 100)  #设置窗口大小
print('窗口尺寸:' + str(driver.get_window_size())) #窗口大小
driver.set_window_position(200, 300) # 设置窗口位置

关闭

driver.quit()
driver.close()

页面基本操作

driver.get(url) # 获取一个页面
driver.back() # 回退
driver.forward() # 前进

demo

import time
from selenium import webdriver

url1 = 'https://www.baidu.com'
url2 = 'https://www.bilibili.com'
driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driverPath)
driver.set_window_position(200, 300)
driver.get(url1)
time.sleep(5)
driver.get(url2)
time.sleep(5)
driver.back()
time.sleep(5)
driver.close()

元素定位以及数据输入

div标签中的属性来进行定位

id,classname,name,tagname,partial link text,link text,css selector

例如下列a标签中的classname为username

driver.find_element_by_class_name("username")

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sp1IfBu5-1641714144496)(C:\Users\29273\AppData\Roaming\Typora\typora-user-images\image-20220109151957230.png)]

driver.find_element_by_class_name("btn.btn_big.btn_error")

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qRsrR3ep-1641714144497)(C:\Users\29273\AppData\Roaming\Typora\typora-user-images\image-20220109152601231.png)]

# 部分函数
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[3]/div[2]/div[1]/dl/dd/div[1]/a')
driver.find_element_by_css_selector('body > div.wrap > div.main_wrap > div.main.cc > div.main_sidebar > div.box_wrap.user_info > dl > dd > div.name > a')
driver.find_element_by_id(value)
driver.find_element_by_name(value)
driver.find_element_by_class_name(value)
driver.find_element_by_tag_name(value)
driver.find_element_by_link_text(value)
driver.find_element_by_partial_link_text(value)

对数据框输入数据

driver.find_element_by_name("username").send_keys("giao")

点击事件

driver.find_element_by_xpath('//*[@id="J_register_form"]/div/dl[5]/dd/button').click()

练习一(简单网页注册)

http://47.107.178.45/phpwind/

import time
from selenium import webdriver

url = r'http://47.107.178.45/phpwind/'
driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driverPath)
driver.get(url)
registerBtn = driver.find_element_by_xpath('//*[@id="J_login_form"]/dl/dd[2]/a')
registerBtn.click()
time.sleep(5)
driver.find_element_by_name("username").send_keys("giao")
driver.find_element_by_name("password").send_keys("giaogiao")
driver.find_element_by_name("repassword").send_keys('giaogiao')
driver.find_element_by_name("email").send_keys('giao@qq.com')
driver.find_element_by_xpath('//*[@id="J_register_form"]/div/dl[5]/dd/button').click()
time.sleep(10)
driver.quit()

练习二

打开某民宿网站->输入城市->选择人数->点击查看详情

import time
from selenium import webdriver

city = input('请输入你想查询的城市:')
adult = input('请输入大人人数:')
teen = input('请输入孩子人人数:')
baby = input('请输入婴儿人数:')

url = "https://www.airbnb.cn/?logo=1"

driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driverPath)
driver.get(url)
city_input = driver.find_element_by_id("Koan-via-HeaderController__input")
city_input.send_keys(city + "\n")

time.sleep(5)

driver.find_element_by_name("query").click()
time.sleep(3)

for i in range(int(adult)):
    driver.find_element_by_xpath(
        '//*[@id="menuItemComponent-guest_picker"]/div/div/div/div[1]/div/div/div[1]/div/div/div/div/div/div[2]/div/div[3]/button').click()
time.sleep(4)
for i in range(int(teen)):
    driver.find_element_by_xpath(
        '//*[@id="menuItemComponent-guest_picker"]/div/div/div/div[1]/div/div/div[2]/div/div/div/div/div/div[2]/div/div[3]/button').click()
time.sleep(3)
for i in range(int(baby)):
    driver.find_element_by_xpath(
        '//*[@id="menuItemComponent-guest_picker"]/div/div/div/div[1]/div/div/div[3]/div/div/div/div/div/div[2]/div/div[3]/button').click()
time.sleep(5)


driver.find_element_by_class_name("_fhph4u")[0].click()
time.sleep(20)

练习三

爬取某站入站必看视频信息

import time
from selenium import webdriver

url = 'https://www.baidu.com'

driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driverPath)
res = driver.find_elements_by_class_name("video-card")
for i in range(len(res)):
    print('第' + str(i) + '个视频')
    print('up主:' + str(res[i].find_element_by_class_name("up-name__text").text))
    print('视频名称:' + str(res[i].find_element_by_class_name("video-name").text))
    print('视频播放量:' + str(res[i].find_element_by_class_name("play-text").text))
    print('评论数:' + str(res[i].find_element_by_class_name("like-text").text))
    print('note:' + str(res[i].find_element_by_class_name("history-hint").text))

练习4

JD买佩奇

import time
from selenium import webdriver


driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driverPath)
driver.get('https://www.jd.com/')
driver.implicitly_wait(5)
driver.maximize_window()
time.sleep(1)

driver.find_element_by_css_selector('a[class="link-login"]').click()
driver.find_element_by_css_selector('b[class="QQ-icon"]').click()
time.sleep(30)
driver.find_element_by_css_selector('input[clstag="h|keycount|head|search_c"]').send_keys('佩奇')
driver.find_element_by_css_selector('button[clstag="h|keycount|head|search_a"]').click()
time.sleep(3)
driver.find_element_by_css_selector('div[class="gl-i-wrap"]').click()
time.sleep(2)

hands = driver.window_handles

driver.switch_to_window(hands[1])

driver.find_element_by_css_selector('a#InitCartUrl').click()

driver.find_element_by_css_selector('a[id="GotoShoppingCart"]').click()
time.sleep(2)

driver.save_screenshot('filename=abc.png')
xxx = driver.window_handles
print(xxx)
driver.switch_to_window(xxx[1])
driver.find_element_by_css_selector('a[class="common-submit-btn"]').click()

time.sleep(90)
driver.quit()

练习5

爬51Job工作信息

import re
import time
from selenium import webdriver

exe = None
i1 = None
i2 = None
i3 = None
i4 = None
edu = None
num = None
location = None
times = 0

driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driverPath)

driver.get(
    'https://search.51job.com/list/000000,000000,0000,00,9,99,%25E8%25BF%2590%25E7%25BB%25B4,2,1.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=')

driver.maximize_window()
time.sleep(1)
all_list = driver.find_element_by_css_selector('div.j_joblist').find_elements_by_css_selector('div.e')

print(len(all_list))

for list in all_list:
    jobname = list.find_element_by_css_selector('div a p span').text
    ublrstime = list.find_element_by_css_selector('span[class="time"]').text
    salary = list.find_element_by_css_selector('span[class="sal"]').text
    d_at = list.find_element_by_css_selector('span[class="d at"]').text
    index1 = re.search("-", salary)
    index2 = re.search("/", salary)

    low_salary = salary[0:int(index1.start())]
    high_salary = salary[int(index1.start()) + 1:index2.start()]
    for i in range(len(d_at)):
        if d_at[i] == ':':
            i4 = i
        if d_at[i] == '|' and times == 0:
            i1 = i
            times += 1
        elif d_at[i] == '|' and times == 1:
            i2 = i
            times += 1
        elif d_at[i] == '|' and times == 2:
            i3 = i
            times += 1
        try:
            exe = d_at[i1 + 1:i2]
            num = d_at[i3 + 1:]
            edu = d_at[i2 + 1:i3 - 1]
            location = d_at[i4:i1]
        except:
            continue
    print('职业名称:{}  发布日期:{}  最低薪资:{} 最高薪资:{} 地区: {} 经验要求:{} 学历:{} 招聘人数:{}'.
          format(jobname, ublrstime, low_salary, high_salary, location, exe, edu, num))

time.sleep(30)
driver.quit()

练习6

爬Tx视频信息

import time
from selenium import webdriver
from selenium.webdriver import ActionChains

url = 'https://v.qq.com/channel/cartoon?listpage=1&channel=cartoon&iarea=1'

driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driverPath)
driver.get(url)
videos = driver.find_elements_by_class_name("list_item")

bo = driver.find_elements_by_class_name('list_item')
for i in range(16):
    a = videos[i].find_element_by_class_name("figure_pic")
    ActionChains(driver).move_to_element(a).perform()
    time.sleep(1)
    det = driver.find_elements_by_xpath('/html/body/div[7]/div/div/div[2]')
    name = det[len(det) - 1].find_element_by_class_name('video_title').find_element_by_tag_name('a').text
    note = bo[i].find_element_by_class_name("figure_desc").text
    info_list = det[len(det) - 1].find_element_by_class_name('video_tags').find_elements_by_tag_name('span')
    info = ''
    for a in info_list:
        info = str(a.text) + ' ' + info
    introduction = det[len(det) - 1].find_element_by_class_name('video_sum').find_elements_by_tag_name('span')[1].text
    print('名称:{}   描述:{}    人物,类型,年:{}   介绍:{}'.format(name, note, info, introduction))

练习7

发邮箱

import time
from selenium import webdriver

url = 'https://mail.qq.com'

driverPath = r'C:\Users\29273\AppData\Local\Programs\Python\Python38-32\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driverPath)
driver.get(url)
time.sleep(5)
driver.switch_to.frame('login_frame')
time.sleep(3)
driver.find_element_by_class_name('face').click()

time.sleep(5)
driver.find_element_by_id("composebtn").click()
time.sleep(5)
driver.switch_to.frame('mainFrame')
# driver.find_element_by_class_name('noime.div_txt').send_keys("2574204651@qq.com")
driver.find_element_by_xpath("//*[@id='toAreaCtrl']/div[2]/input").send_keys('2574204651@qq.com')
time.sleep(2)
driver.find_element_by_xpath('/html/body/form[2]/div[2]/div[3]/table[3]/tbody/tr[2]/td[2]/div/div/div/input').send_keys(
    "去死吧~~~")
time.sleep(2)
driver.switch_to.default_content()
driver.switch_to.frame('mainFrame')
frame_boby = driver.find_element_by_xpath('//iframe[@scrolling="auto"]')

driver.switch_to.frame(frame_boby)
time.sleep(2)
driver.find_element_by_xpath('/html/body').send_keys("去死吧!!!")
driver.switch_to.parent_frame()
driver.find_element_by_name('sendbtn').click()
time.sleep(10)
driver.close()

练习8

QQMc热度

import time
from selenium import  webdriver

driver= webdriver.Chrome(executable_path='D:\PythonTest\pythonProject\driver\chromedriver.exe')
driver.get('https://y.qq.com/n/ryqq/toplist/62')
driver.implicitly_wait(5)
driver.maximize_window()
time.sleep(1)

all_songlist = driver.find_element_by_css_selector('ul.songlist__list ').find_elements_by_css_selector('li')
print(len(all_songlist))

for songlist in all_songlist:
    songlist__number = songlist.find_element_by_css_selector('div.songlist__number ').text
    songlist__rank = songlist.find_element_by_css_selector('div.songlist__rank').text
    author = songlist.find_element_by_css_selector('a.playlist__author').text
    songlist__time=songlist.find_element_by_css_selector('div.songlist__time').text
    songlist__songname=songlist.find_element_by_css_selector('span.songlist__songname_txt a+a').text
    print('排名:{}   热度:{}   歌曲名称:{}   歌手: {}   时长: {}'
          .format(songlist__number, songlist__rank, songlist__songname, author, songlist__time))

练习八

csdn发博客


import time
from selenium import  webdriver

driver= webdriver.Chrome(executable_path='D:\PythonTest\pythonProject\driver\chromedriver.exe')
driver.get('https://www.csdn.net/')
driver.implicitly_wait(5)
driver.maximize_window()
time.sleep(1)

driver.find_element_by_css_selector('div[class="toolbar-btn toolbar-btn-login csdn-toolbar-fl "]').click()

#本来是准备用用户名和密码登入,验证无法通过只能手动扫码
# time.sleep(2)
# driver.find_element_by_css_selector('div[class="login-box-tabs-items"] span+span+span+span').click()
# time.sleep(2)
# driver.find_element_by_css_selector('input.base-input-text').send_keys('xxxx')
# time.sleep(2)
# driver.find_element_by_css_selector('input[placeholder="密码"]').send_keys('xxxxx')
# time.sleep(2)
# driver.find_element_by_css_selector('button.base-button').click()
time.sleep(7)

driver.find_element_by_css_selector('div.toolbar-btns.onlyUser div+div+div+div+div+div').click()
time.sleep(2)
driver.find_element_by_css_selector('div.createBtn').click()
time.sleep(2)
driver.find_element_by_css_selector('button.btn.btn-publish').click()
time.sleep(2)
driver.find_element_by_css_selector('div.switch-box label').click()
time.sleep(2)
driver.find_element_by_css_selector('div.modal__button-bar button+button+button+button').click()
  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2022-01-12 00:19:40  更:2022-01-12 00:21:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/18 5:39:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码