selenium自动化测试简单准备

Posted 沃特艾文儿~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()

基本函数

尺寸,位置

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")

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

# 部分函数
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')Selenium学习笔记 - 自动化测试体系与原理

Python 使用 selenium 进行自动化测试 或者协助日常工作

小白学 Python 爬虫(28):自动化测试框架 Selenium 从入门到放弃(下)

小白学 Python 爬虫(27):自动化测试框架 Selenium 从入门到放弃(上)

资深和新手的100大 Selenium面试问答

面试准备——selenium常见面试题及答案(java)