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('[email protected]')
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))

到此這篇關於selenium自動化測試簡單準備 的文章就介紹到這瞭,更多相關selenium自動化測試內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: