1. 程式人生 > 實用技巧 >selenium自動化知識點整理(部分,py版)

selenium自動化知識點整理(部分,py版)

#chromedriver各版本下載地址:http://chromedriver.storage.googleapis.com/index.html

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.support.select import Select

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver import ActionChains
import random
import time

'''------------------------------------------------設定header----------------------------------------'''
# 獲取請求頭物件,該物件用於設定使用者向瀏覽器傳送請求的請求頭,比如ip代理設定瀏覽器代理設定及瀏覽器後臺執行等等
chrome_options = Options()
#此處設定了瀏覽器代理為H5的,這樣開啟的頁面就跟用手機訪問的是一樣的
# chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36")
# chrome_options.add_argument('--user-data-dir=C:/Users/28546/AppData/Local/Google/Chrome/User Data')#使用使用者瀏覽器資料啟動
# chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) #去掉正受自動化測試軟體控制的提示語


chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_argument("referer=https://www.zhipin.com/shanghai/")
# 此處程式碼設定瀏覽器為後端執行,不會呈現出來
# chrome_options.add_argument("--headless")

#使用代理ip來發請求
# chrome_options.add_argument("--proxy-server=http://127.0.0.1:8080")
'''--------------------------------------------------瀏覽器操作----------------------------------'''
#獲取瀏覽器物件,這個就好比人雙擊chrome瀏覽器,其中1、executable_path:為chromedriver的路徑 2、options:為請求頭物件

browser = webdriver.Chrome(executable_path="C:\chromedriver.exe",options=chrome_options)
# browser.execute_script('Object.defineProperties(navigator, {webdriver:{get:()=>undefined}});')
#開啟瀏覽器

# url = "https://login.taobao.com/member/login.jhtml?spm=a21bo.2017.754894437.1.5af911d9W5Eadh&f=top&redirectURL=https%3A%2F%2Fwww.taobao.com%2F"
# browser.get(url=url)
browser.get("https://passport.alibaba.com/member/reg/enter_fill_email.htm?_regfrom=TB_ENTERPRISE&_lang=")
#瀏覽器最大/小化
browser.maximize_window()
# browser.minimize_window()

#獲取網頁原始碼
# text = browser.page_source

#開啟一個新的頁面
# newwindow = 'window.open("https://www.baidu.com")'
# browser.execute_script(newwindow) #通過執行js指令碼來開啟一個新的頁面
# print(browser.window_handles) #遍歷開啟的所有視窗控制代碼
# print(browser.current_window_handle) #檢視當前頁面控制代碼
# browser.switch_to.window(browser.window_handles[0]) #切換頁面
# browser.close() #關閉當前頁面
'''------------------------------cookie操作-----------------------'''
# browser.add_cookie({"name":"test","value":"111111"}) #設定cookie
# browser.delete_all_cookies()
# browser.add_cookie({"name":"lastCity","value":"101020100"})
# browser.add_cookie({"name":"__c","value":"1578881583"})
# browser.add_cookie({"name":"__g","value":"-"})
# browser.add_cookie({"name":"Hm_lvt_194df3105ad7148dcf2b98a91b5e727a","value":"1578881583"})
# browser.add_cookie({"name":"__l","value":"l=%2Fwww.zhipin.com%2Fshanghai%2F&r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DIBYCElg2InhaLCPnxncuQ2zc93xIiq_0D0QMsdwDqEShPmm-r66Zh_Ir0idbvQqK%26wd%3D%26eqid%3Ded7391010018a58e000000055e1bd22a&friend_source=0&friend_source=0"})
# browser.add_cookie({"name":"JSESSIONID","value":'""'})
# browser.add_cookie({"name":"_uab_collina","value":"157888421120766492060183"})
# browser.add_cookie({"name":"__zp_stoken__","value":"357fKm%2FOlK%2BCYcOmu8JSarqolf2816dErbTZvArsZYQeXw9vI%2BDV%2FFAwg8ClcW5AwXDkFRVWSPkPckalpflNMachjua4ibtVyXK8oeMcUKaxyE0amYJAyqupK%2Fv3CmH914vZ"})
# browser.add_cookie({"name":"toUrl","value":"/"})
# browser.add_cookie({"name":"__a","value":"1117176.1578881583..1578881583.68.1.68.68"})
# browser.add_cookie({"name":"Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a","value":"1578972610"})

print(browser.get_cookies())
# cookies = browser.get_cookies() #遍歷cookie
# browser.delete_cookie("test") #刪除cookie
# browser.delete_all_cookies() # 刪除所有cookie

'''------------------------------input框操作-----------------------'''
# label_element = browser.find_element_by_id("kw") #通過id查詢元素
# label_element = browser.find_element_by_class_name("s_ipt") #通過類目查詢元素
# label_element = browser.find_element_by_tag_name("") #通常標籤名稱查詢元素
# label_element = browser.find_element_by_name("wd") #通過name查詢名稱元素
# label_element = browser.find_element_by_xpath("//input[@id='kw']") #通過xpath來查詢元素

# button_element = browser.find_element_by_id("su")
# button_element.click() #按鈕點選動作
'''------------------------------select框--------------------------'''
# select_element = Select(browser.find_element_by_id("searchHotelLevelSelect"))
# for option in select_element.options:
# print(option.text) #列印文字
# print(option.get_attribute("value")) #列印屬性
# select_element.select_by_index(2) #通過文字選擇
# select_element.select_by_value("3") #通過value選擇
# select_element.select_by_visible_text("二星級以下/經濟") #通過文字選擇
'''-------------------------------元素等待(隱私等待和顯示等待)----------'''
# 1、隱式等待
# slip_element = browser.implicitly_wait(3)
# browser.find_element_by_class_name("nc-lang-cnt")
#2、顯示等待
# try:
# slip_element = WebDriverWait(browser,10).until(
# EC.presence_of_element_located((By.CLASS_NAME,"nc-lang-cnt1")) #這裡還有其他各種定位方式,檢視提供的方法說明即可
# )
# except:
# print("--------")

'''--------------------------------------元素拖拽(行為鏈)--------------------------'''
#https://signup.zhipin.com/?ka=header-register boss直聘的網站案例
# while True:
# action_chains = ActionChains(browser) # 建立一個行為鏈
# spans = browser.find_elements_by_xpath("//span[@class='nc_iconfont btn_slide']")
# try:
# for span in spans:
# if span.size.get("height") > 0:
# action_chains.drag_and_drop_by_offset(span,258,0).perform()
# break
# except:
# print('---')
#
# browser.refresh()
# time.sleep(3)

browser.quit()