文章目錄
- 定位一組元素
- 多層框架/視窗定位
- 多層視窗定位
- 下拉框處理
- 提示框處理
- DIV對話框
- 上傳檔案操作
定位一組元素
- ebdriver 可以很方便的使用findElement 方法來定位某個特定的物件,不過有時候我們卻需要定位一組物件,這時候就需要使用findElements 方法,
- 定位一組物件一般用于以下場景:
- 批量操作物件,比如將頁面上所有的checkbox 都勾上
- 先獲取一組物件,再在這組物件中過濾出需要具體定位的一些物件,比如定位出頁面上所有的checkbox,然后選中點擊
- get_attribute:獲得屬性值,
from selenium import webdriver
import time
import os
driver = webdriver.Chrome()
# 打開本地html檔案
path = "file:///" + os.path.abspath("D:/Tool/%E6%B5%8B%E8%AF%95%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B71/selenium2html/checkbox.html")
driver.get(path)
# 獲取頁面上一組的input元素 然后逐一進行判斷
inputs = driver.find_elements_by_tag_name("input")
for input in inputs:
if input.get_attribute("type") == 'checkbox':
input.click()
time.sleep(2)
多層框架/視窗定位
- 多層框架或視窗的定位:
switch_to.frame()
switch_to.window() - 對于一個現代的web 應用,經常會出現框架(frame) 或視窗(window)的應用,這也就給我們的定位帶來了一個難題,有時候我們定位一個元素,定位器沒有問題,但一直定位不了,這時候就要檢查這個元素是否在一個frame 中,
seelnium webdriver 提供了一個switch_to.frame 方法,可以很輕松的來解決這個問題,
switch_to.frame(name_or_id_or_frame_element): - 可以簡單記憶一下,如果這個frame有name和id屬性那么就用這兩個屬性就好,如果沒有的話可以先用 find_element.by_xxx方法找到這個frame元素,然后把這個元素傳進去,這也是可行的,
switch_to.frame()把當前定位的主體切換了frame里,怎么理解這句話呢?我們可以從frame的實質去理解,
frame中實際上是嵌入了另一個頁面,而webdriver每次只能在一個頁面識別,因此才需要用switch_to.frame方法
去獲取frame中嵌入的頁面,對那個頁面里的元素進行定位, - switch_to_default_content:從frame中嵌入的頁面里跳出,跳回到最外面的原始頁面中,
from selenium import webdriver
import time
import os
driver = webdriver.Chrome()
# 打開本地html檔案
path = "file:///" + os.path.abspath("D:/Tool/%E6%B5%8B%E8%AF%95%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B71/selenium2html/frame.html")
driver.get(path)
driver.implicitly_wait(30)
# 進入下一級框架
driver.switch_to.frame("f1")
driver.switch_to.frame("f2")
driver.find_element_by_id("kw").send_keys("詹姆斯")
driver.find_element_by_id("su").click()
# 回到初始框架
driver.switch_to_default_content()
# 再重新進去
driver.switch_to.frame("f1")
driver.implicitly_wait(30)
driver.close()
多層視窗定位
有可能嵌套的不是框架,而是視窗,還有真對視窗的方法:switch_to_window
用法與switch_to_frame 相同:
driver.switch_to_window(“windowName”)
from selenium import webdriver
from selenium.webdriver.support.ui import webDriverwait
import time
import os
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('level_locate.html')
dr.get(file_path)
# 點擊Link1鏈接(彈出下拉串列)
dr.find_element_by_link_text('Link1').click()
# 找到id 為dropdown1的父元素
webDriverwait(dr,10).until(lambda the_driver:
the_driver.find_element_by_id('dropdown1 ').is_displayed())
# 在父親元件下找到link為Action的子元素
menu = dr.find_element_by_id('dropdown1').find_element_by_link_text('Action')
# 滑鼠定位到子元素上
webdriver.ActionChains(dr).move_to_element(menu) .perform()
time.sleep(2)
dr.quit()
下拉框處理
- 下拉框是我們最常見的一種頁面元素,對于一般的元素,我們只需要一次就定位,但下拉框里的內容需要進行兩次定位,先定位到下拉框,再定位到下拉框內里的選項,
import os
import time
from selenium import webdriver
driver = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath("D:/Tool/%E6%B5%8B%E8%AF%95%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B71/selenium2html/drop_down.html")
driver.get(file_path)
time.sleep(2)
# 先定位到下拉框
m = driver.find_element_by_id("ShippingMethod")
# 再點擊下拉框下的選項
# m.find_element_by_xpath("//option[@value='10.69']").click()
# 通過獲取一組元素按陣列中的位置去選擇下拉框
options = driver.find_elements_by_tag_name("option")
options[2].click()
time.sleep(3)
driver.quit()
提示框處理
text 回傳alert/confirm/prompt 中的文字資訊
accept 點擊確認按鈕
dismiss 點擊取消按鈕,如果有的話
send_keys 輸入值,這個alert\confirm 沒有對話框就不能用了,不然會報錯
#接受警告資訊
alert = dr.switch_to_alert()
alert.accept()
#得到文本資訊列印
alert = dr.switch_to_alert()
print alert.text
#取消對話框(如果有的話)
alert = dr.switch_to_alert()
alert.dismiss()
#輸入值
alert = dr.switch_to_alert()
alert.send_keys("hello word")
- 注意:switch_to.alert()只能處理原生的alert
from selenium import webdriver
import time
import os
dr = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath("D:/Tool/%E6%B5%8B%E8%AF%95%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B71/selenium2html/alert.html#")
dr.get(file_path)
# 點擊鏈接彈出alert
dr.find_element_by_link_text("hover to see tooltip").click()
time.sleep(3)
# 接受警告資訊
alert = dr.switch_to.alert()
alert.accept()
# 得到文本資訊列印
print(alert.text)
time.sleep(2)
dr.quit()
DIV對話框
- 更多的時候我們在實際的應用中碰到的并不是簡單警告框,而是提供更多功能的會話框,
from selenium import webdriver
from time import sleep
import os
dr = webdriver.Chrome()
file_path = "file:///D:/Tool/%E6%B5%8B%E8%AF%95%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B71/selenium2html/modal.html"
dr.get(file_path)
# 打開對話框
dr.find_element_by_id('show_modal').click()
sleep(3)
# 點擊對話框中的鏈接
link = dr.find_element_by_id('myModal').find_element_by_id('click')
link.click()
sleep(4)
# 關閉對話框
buttons = dr.find_element_by_class_name('modal-footer').find_elements_by_tag_name('button')
buttons[0].click()
sleep(2)
dr.quit()
上傳檔案操作
- 檔案上傳操作也比較常見功能之一,上傳功能沒有用到新有方法或函式,關鍵是思路,上傳程序一般要打開一個本地視窗,從視窗選擇本地檔案添加,所以,一般會卡在如何操作本地視窗添加上傳檔案,
- 其實,在selenium webdriver 沒我們想的那么復雜;只要定位上傳按鈕,通過send_keys 添加本地檔案路徑就可以了,絕對路徑和相對路徑都可以,關鍵是上傳的檔案存在,
from selenium import webdriver
import os, time
driver = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath("D:/Tool/%E6%B5%8B%E8%AF%95%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B71/selenium2html/upload.html")
driver.get(file_path)
# 定位上傳按鈕,添加本地檔案
driver.implicitly_wait(30)
driver.find_element_by_name("file").send_keys("D:/test/object.txt")
time.sleep(10)
driver.quit()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/185011.html
標籤:其他
上一篇:機器學習十大經典演算法:樸素貝葉斯影像分割實戰——Nemo魚影像分割(python代碼+詳細注釋)
下一篇:自制爬蟲框架
