溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Selenium2相關(guān)知識(shí)最全總結(jié)

發(fā)布時(shí)間:2020-08-26 14:07:10 來(lái)源:網(wǎng)絡(luò) 閱讀:524 作者:o鳳舞九天o 欄目:軟件技術(shù)

1. 定位元素的幾種方法

????find_element_by_id(優(yōu)先,常用),對(duì)應(yīng)的是id屬性

????find_element_by_name(優(yōu)先,常用),對(duì)應(yīng)的是name屬性

????find_element_by_class_name,對(duì)應(yīng)的是class屬性的值

????find_element_by_tag_name,對(duì)應(yīng)的是標(biāo)簽名稱,如input,p,span,title,body....

????find_element_by_link_text,對(duì)應(yīng)的是超鏈接的文本內(nèi)容

????find_element_by_partial_link_text,對(duì)應(yīng)的是部分超鏈接的文本內(nèi)容

????find_element_by_xpath,對(duì)應(yīng)的是元素的path,可通過(guò)firebug工具獲得

????find_element_by_css_selector,通過(guò)css屬性定位,class用.? id用#,

????? ? ?示例:

????? ? ? ? ?driver.find_element_by_css_selector(".s_ipt").send_keys("selenium")

????? ? ? ? ?driver.find_element_by_css_selector("#su").click()


2.By定位

????find_element(By.ID,"kw")

????find_element(By.NAME,"wd")

????find_element(By.CLASS_NAME,"s_ipt")

????find_element(By.TAG_NAME,"input")

????find_element(By.LINK_TEXT,u"新聞")

????find_element(By.PARTIAL_LINK_TEXT,u"新")

????find_element(By.XPATH,"http://*[@class='bg s_btn']")

????find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su")?


3.幾種瀏覽器操作

? ? 說(shuō)明:操作瀏覽器需要將瀏覽器插件目錄放入環(huán)境變量中,不然找不到

? ? from selenium import webdriver

? ? driver = webdriver.Ie()? ? ? ? --IE瀏覽器,插件名為? ?IEDriverServer.exe

? ? driver = webdriver.Firefox()? ?--?火狐瀏覽器,插件名為? geckodriver.exe

? ? driver = webdriver.Chrome()? --?谷歌瀏覽器,插件名為? chromedriver.exe

? ? driver = webdriver.Edge()? ? ? --Edge瀏覽器(windows10系統(tǒng)自帶),?無(wú)需插件

? ? 操作360瀏覽器:

? ? ? ?360瀏覽器內(nèi)核基于chrome瀏覽器,因此插件用的也是chromedriver.exe,不過(guò)得配置下

????????chrome_options = webdriver.ChromeOptions()

????????chrome_options.binary_location = r"C:\Users\kevin\AppData\Roaming\360se6\Application\360se.exe" #這里是360安全瀏覽器的路徑

????????chrome_options.add_argument(r'--lang=zh-CN') # 這里添加一些啟動(dòng)的參數(shù)

????????d = webdriver.Chrome(chrome_options=chrome_options)

? ?

4.控制瀏覽器

? ?設(shè)置寬高:driver.set_window_size(400,500),? ------設(shè)置寬400,高500

? ?設(shè)置全屏:driver.maximize_windows()

? ?前進(jìn):driver.forward()

? ?后退:? driver.back()

? ?刷新當(dāng)前頁(yè)面:driver.refresh()

? ?

5.定位到的元素操作

? ?driver.find_element_by_xx('xx').click()? ? ? ? ? ? ? --點(diǎn)擊操作

? ?driver.find_element_by_xx('xx').clear()? ? ? ? ? ? ?--清除操作,清除文本框中的內(nèi)容

? ?driver.find_element_by_xx('xx').send_keys('123')? ? ?--輸入數(shù)據(jù)到文本框

? ?driver.find_element_by_xx('xx').submit()? ? ? ? ?--提交操作,相當(dāng)于回車,如在搜索框中輸入內(nèi)容后,執(zhí)行此方法,可直接執(zhí)行搜索動(dòng)作

? ?driver.find_element_by_xx('xx').size()? ? ? ? ? ? ? --獲取元素的尺寸

? ?driver.find_element_by_xx('xx').text? ? ? ? ? ? ? ? --獲取元素的文本

? ?driver.find_element_by_xx('xx').get_attribute('id')? ?--獲取id屬性,只要是屬性都可通過(guò)此方法獲取,如name,id,type,class.

? ?driver.find_element_by_xx('xx').is_displayed()? ? --獲取元素是否可見(jiàn),可見(jiàn)返回True,否則False

? ?

6.鼠標(biāo)事件

? ?Actions類中提供了鼠標(biāo)操作的常用方法

? ?perform()? ? ? ? ? ? ? ? ? ?????????????--執(zhí)行所有Actions類中存儲(chǔ)的行為

? ?context_click()? ? ? ? ? ? ? ? ? ? ? ?--右擊

? ?double_click()????????????????????? ? --雙擊

? ?drap_and_drop()????????????????? ?--拖動(dòng)

? ?move_to_element???????????????? --懸停


? 調(diào)用示例:

? ? ?context_click()? ? ? ?

from?selenium.webdriver.common.action_chains?ActionChains

driver?=?webdriver.Ie()
right_click?=?driver.find_element_by_id()
ActionChains(driver).context_click(right_click).perform()

? ? 其他的方法調(diào)用同上,只是改下方法而已


7.鍵盤操作

? ?模擬組合鍵 CTRL+A,CTRL+C等等

? ?from selenium.webdriver.common.keys import Keys

? ?退格鍵 Keys.BACK_SPACE? ? ? ? ? ? ?---? driver.find_elemennt_by_id('').send_keys(Keys.BACK_SPACE)

? ?空格鍵Keys.SPACE????????????????????????---? driver.find_elemennt_by_id('').send_keys(Keys.SPACE)

? ?輸入CTRL+A? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?---? driver.find_elemennt_by_id('').send_keys(Keys.CONTROL,'a')

? ?回車鍵????????????????????????????????????????? ?---? driver.find_elemennt_by_id('').send_keys(Keys.ENTER)


8.設(shè)置元素等待

? ?顯式等待:

????????????一直檢測(cè)并等待某個(gè)元素出現(xiàn)或消失,達(dá)到最大時(shí)長(zhǎng)條件還未成立則拋出超時(shí)異常

????????? ?from selenium import webdriver

????????? ?from selenium.webdriver.support.ui import WebDriverWait

????????? ?from selenium.webdriver.support import expected_conditions as EC

????????? ?driver = webdriver.Ie()
? ? ? ? ? ?driver.get("https://www.baidu.com")

????????? ? element = WebDriverWait(driver,5,0.5).until(

????????? ????????????????????EC.presence_of_?element_located((By.id,"kw"))? ? ?

????????? ? ? ?)

????????? ?element.send_keys('hello')

????????? ?5:表示最長(zhǎng)等待時(shí)間

????????? ?0.5:檢測(cè)間隔時(shí)間,也就是隔多長(zhǎng)時(shí)間檢測(cè)一次,單位秒

????????? ?until:直到括號(hào)中條件成立,也存在until not

? ? 隱式等待:

? ? ? ? ? ? driver.implicitly_wait(10),單位為秒,不設(shè)置默認(rèn)為0,一般寫在用例開頭,針對(duì)整條用例有效,在定位一個(gè)元素時(shí),如果元素未加載出來(lái),則定位不到。此方? ? ? ? ? ? ? 法會(huì)在設(shè)定時(shí)間內(nèi)一直檢測(cè),檢測(cè)到了就向下進(jìn)行,否則就拋出超時(shí)異常

? ? sleep休眠:

? ? ? ? ? ? ?強(qiáng)制等待一段時(shí)間

? ? ? ? ? ? ?import time

? ? ? ? ? ? ?time.sleep(10)


9.定位一組元素

? ?find_elements_by_xx,同定位單個(gè)元素一樣,不過(guò)element是elements

? ?示例:勾選全部checkbox

? ? checkboxs = driver.find_elements_by_tag_name("input")

? ? for checkbox in checkboxs:

? ? ? ? ? if checkbox.get_attribute("type") == "checkbox":

? ? ? ? ? ? ? checkbox.click()?


10.操作下拉選擇框

? ?方法:

? ? ? 選擇:

? ? ? ? ? ?select_by_index()? ?--通過(guò)索引

? ? ? ? ? ?select_by_value()? ?--通過(guò)value值

? ? ? ? ? ?select_by_visiable_text()? --通過(guò)顯示的文本

? ? ? 反選:

????????? ?deselect_by_index()? ?--通過(guò)索引

? ? ? ? ? ?deselect_by_value()? ?--通過(guò)value值

? ? ? ? ? ?deselect_by_visiable_text()? --通過(guò)顯示的文本

? ? ? ? ? ?deselect_all()? ? ? ? ? ? ? --取消選擇所有

? ? ?示例:

? ? ? ? ? ? ? ? ? <select id="sid">

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <option></option>

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <option value="o1" id="id1">顯示文本1</option>

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <option value="o2" id="id2">顯示文本2</option>

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <option value="o3" id="id3">顯示文本3</option>

? ? ? ? ? ? ? ? ? ?</select>

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ?選擇示例:

? ? ? ? ? ? ? ? ? ? ? ? select_by_index(1)? ?--通過(guò)索引

? ? ? ? ? ? ? ? ? ? ? ? select_by_value('o2' )? ?--通過(guò)value值

? ? ? ? ? ? ? ? ? ? ? ? select_by_visiable_text('顯示文本2')? --通過(guò)顯示的文本

? ? ? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? 完整示例:

? ? ? ? ? ? ? ? ? from selenium.webdriver.support.ui import Select

? ? ? ? ? ? ? ? ? select = Select(driver.find_element_by_id("sid"))

? ? ? ? ? ? ? ? ? select.select_by_index(1)

? ? ? ? ? ? ? ? ??

11. 多表單切換

? ? ??

? ? ? 許多頁(yè)面存在表單嵌套情況,從一個(gè)表單是無(wú)法直接定位到其他表單中元素的,需要進(jìn)行表單切換

? ? ? f = driver.find_element_by_id("fr")? ?--定位到id為fr的表單,定位方法通用?

? ? ? driver.switch_to.frame(f)? ? ?---切換到id為fr的表單中

? ? ? 操作完成后跳到最外層表單: driver.switch_to.default_content()

? ? ??

12.多窗口切換

? ? ? 獲取當(dāng)前窗口的handle:? ?current_handle = driver.current_window_handle

? ? ? 獲取所有窗口handle:? ? handles = driver.window_handles

? ? ? 切換到其他窗口

? ? ? for hand in handles:

? ? ? ? ? ? ?if hand != current_handle:

? ? ? ? ? ? ? ? ?driver.swith_to.window(hand)? ? ? ? ? ?--切換窗口

? ? ? ? ? ? ? ? ?

13.彈窗處理

? ? ? 針對(duì)alert,prompt,confirm出現(xiàn)的彈窗,可使用driver.switch_to_alert()進(jìn)行捕獲并處理

? ? ? 方法:

? ? ? ? ? ?text? ? ? ? ? ? ? ? ? ? ? ? ? ? --返回彈窗的文本信息

? ? ? ? ? ?accept(),? ? ? ? ? ? ? ? ? ?--接受彈窗,也就是點(diǎn)擊確定

? ? ? ? ? ?dismiss()? ? ? ? ? ? ? ? ? --取消彈窗

? ? ? ? ? ? ? ? ??

? ? ? 調(diào)用示例:

? ? ? ? ? ? ?driver.switch_to_alert().accept()? ? ? ? ? --點(diǎn)擊確定

? ? ? ? ? ? ?

14.上傳文件

? ? ? 方法一:直接通過(guò)send_keys()方法輸入文件路徑到文本框中,點(diǎn)擊提交按鈕

? ? ? 方法二:通過(guò)AutoIt工具,將操作過(guò)程打包成exe文件,然后通過(guò)python進(jìn)行調(diào)用

? ? ??

15.操作cookie

? ? ?獲取所有cookie:? ? ? ? ? ? ? ? ? ? ? ? ? driver.get_cookies()

? ? ?獲取名為xx的cookie:? ? ? ? ? ? ? ? ? ?driver.get_cookie('xx')

? ? ?添加cookie:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?driver.add_cookie(dict)? ? -- 參數(shù)為字典對(duì)象

? ? ?刪除所有cookie:? ? ? ? ? ? ? ? ? ? ? ? ? driver.delete_all_cookies()

? ? ?

16.調(diào)用JavaScript

? ? ? js = "window.scrollTo(100,450);"

? ? ? driver.execute_script(js)

? ? ??

17.處理HTM5視頻播放

? ? ?示例:

? ? ? ? ?video = driver.find_element_by_xpath("body/Section[1]/div/video")

? ? ? ? ?driver.execute_script("return arguments[0].play()",video)? ? ? ? ? ? ? ? ? ? ? ? ? --播放

? ? ? ? ?driver.execute_script("return arguments[0].pause()",video)? ? ? ? ? ? ? ? ? ? ? ?--暫停

? ? ? ? ??


18.窗口截圖


? ? ? driver.get_screenshot_as_file("D:\xxx\xx.png")


19.關(guān)閉窗口


? ? driver.close()? ---關(guān)閉當(dāng)前窗口

? ? driver.quit()? ? ---關(guān)閉所有窗口


20.驗(yàn)證碼處理

? ? ?1.去掉驗(yàn)證碼

? ? ?2.設(shè)置萬(wàn)能驗(yàn)證碼

? ? ?3.使用cookie

? ? ?4.驗(yàn)證碼識(shí)別

? ? ?5.驗(yàn)證碼存放在cookie中? ?


21.操作已經(jīng)打開的瀏覽器,下面以chrome為例

? ? 1.手動(dòng)啟動(dòng)瀏覽器,以debug模式啟動(dòng)

? ? ? C:\Program Files (x86)>chrome.exe --remote-debugging-port=9222

? ? 2.添加selenium配置

? ? ? options = webdriver.ChromeOptions()

? ? ? options.debugger_address(options)

? ? ? driver = webdriver.Chrome(options)

? ? ? 剩下的按照之前正常操作就可以了

? ? ?




遇到的問(wèn)題及解決方法:

1.報(bào)如下錯(cuò)誤

selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'

? (Session info: chrome=75.0.3770.142)

? (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.17134 x86_64)

? ?解決方案:

? ? 這是由于chromedriver和chrome瀏覽器版本不匹配導(dǎo)致的,匹配信息如下:

? ??

chromedriver版本支持的Chrome版本
v2.41v67-69
v2.40v66-68
v2.39v66-68
v2.38v65-67
v2.37v64-66
v2.36v63-65
v2.35v62-64
v2.34v61-63
v2.33v60-62
v2.32v59-61
v2.31v58-60
v2.30v58-60
v2.29v56-58
v2.28v55-57
v2.27v54-56
v2.26v53-55
v2.25v53-55
v2.24v52-54
v2.23v51-53
v2.22v49-52
v2.21v46-50
v2.20v43-48
v2.19v43-47
v2.18v43-46
v2.17v42-43
v2.13v42-45
v2.15v40-43
v2.14v39-42
v2.13v38-41
v2.12v36-40
v2.11v36-40
v2.10v33-36
v2.9v31-34
v2.8v30-33
v2.7v30-33
v2.6v29-32
v2.5v29-32
v2.4v29-32

? ? ?下載地址:http://npm.taobao.org/mirrors/chromedriver/

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

? ? ?如果chrome版本太高,建議使用IE瀏覽器代替

?

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI