Selenium(十):用By定位元素、鼠標事件、鍵盤事件

1. 用By定位元素

除了前面介紹的單位方法,WebDriver還提供了另一套寫法,即統一調用find_element()方法,經過By來聲明定位的方法,而且傳入對應定位方法的定位參數。具體以下:web

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

wd = webdriver.Chrome()

wd.get('https://www.baidu.com/')

wd.find_element(By.ID,"kw")
wd.find_element(By.NAME,"wd")
wd.find_element(By.CLASS_NAME,"s_ipt")
wd.find_element(By.TAG_NAME,"input")
wd.find_element(By.LINK_TEXT,u"新聞")
wd.find_element(By.PARTIAL_LINK_TEXT,u"")
wd.find_element(By.XPATH,"//*[@class='bg s_btn']")
wd.find_element(By.CSS_SELECTOR,"span.bg.s_btn_wr>input#su")

find_element()方法只用於定位元素。它須要兩個參數,第一個參數是定位的類型,由By提供:第二個參數是定位的具體方式。在使用By以前須要將By類導入。瀏覽器

from selenium.webdriver.common.by import By

經過查看WebDriver的底層實現代碼發現它們實際上是一回事兒,例如,find_element_by_id()方法的實現。函數

def find_element_by_id(self, id_):
    """Finds an element by id.

    :Args:
    - id\_ - The id of the element to be found.

    :Returns:
    - WebElement - the element if it was found

    :Raises:
    - NoSuchElementException - if the element wasn't found

    :Usage:
        element = driver.find_element_by_id('foo')
    """
    return self.find_element(by=By.ID, value=id_)

但WebDriver更推薦前面介紹的寫法,固然我公司底層是使用By來分裝函數的。網站

2. 鼠標事件

前面不但使用過click()方法,並且還了解了下其餘鼠標交互方式,例如鼠標點擊、雙擊、懸停、甚至是鼠標拖動等功能。spa

在這一章中,我將詳細說明這些功能。設計

在WebDriver中,將這些關於鼠標操做的方法封裝在ActionChains類提供。code

ActionChains類提供了鼠標操做的經常使用方法:orm

perform():執行全部ActionChains中存儲的行爲blog

context_click():右擊教程

double_click():雙擊

drag_and_drop():拖動

move_to_element():鼠標懸停

2.1 鼠標右擊操做

對於ActionChains類所提供的鼠標方法與前面學過的click()方法的用法有所不一樣。

我想了半天,愣是沒想到哪一個網站能夠右鍵,因此沒辦法弄出實例了。

導入提供鼠標操做的ActionChains:

from selenium.webdriver import ActionChains

調用ActionChains(driver)類,將瀏覽器驅動driver做爲參數傳入,我通常是用wd做爲瀏覽器驅動的名稱。

ActionChains(wd)

context_click(right)方法用於秒你鼠標右鍵操做,在調用是須要制定元素定位。

ActionChains(wd).context_click(right)

執行全部ActionChains中存儲的行爲,能夠理解成是對整個操做的提交動做。

ActionChains(wd).context_click(right).perform()

2.2 鼠標懸停彈出下拉菜單是一個十分建立的功能設計。

move_to_element()方法能夠模擬鼠標懸停的動做,其用法與context_click()相同。

from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains

wd = webdriver.Chrome()

wd.get('https://www.baidu.com/')

#定位到要鼠標懸停的元素
above = wd.find_element_by_xpath("//div[@id='u1']/a[8]")
#對定位到的元素執行鼠標懸停操做
ActionChains(wd).move_to_element(above).perform()

2.3 鼠標雙擊操做

double_click方法用於秒你鼠標雙擊操做。和前面的使用方法如出一轍,就不用代碼演示了。

2.4 鼠標拖放操做

drag_and_drop(source,target)在源元素上按住鼠標左鍵,而後移動到目標元素上釋放。

source:鼠標拖動的源元素

target:鼠標釋放的目標元素

這個也沒想到哪裏可使用到,可能後面的滑動式驗證碼須要使用。

3. 鍵盤事件

Keys()類提供了鍵盤上幾乎全部按鍵的方法。前面瞭解到,send_keys()方法能夠用來模擬鍵盤輸入,除此以外,咱們還能夠用它來輸入鍵盤上的按鍵,甚至是組合鍵,如Ctrl+A、Ctrl+C等。

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

wd = webdriver.Chrome()

wd.get('https://www.baidu.com/')

#輸入框輸入內容
wd.find_element_by_id("kw").send_keys("seleniumm")

#刪除多輸入的一個m
wd.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)

#輸入空格鍵+「教程」
wd.find_element_by_id("kw").send_keys(Keys.SPACE)
wd.find_element_by_id("kw").send_keys("教程")

#Ctrl+a 全選輸入框內容
wd.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')

#Ctrl+x 剪切輸入框內容
wd.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')

#Ctrl+v 粘貼輸入框內容
wd.find_element_by_id("kw").send_keys(Keys.CONTROL,'v')

#經過回車鍵來代替單擊操做
wd.find_element_by_id("su").send_keys(Keys.ENTER)

須要說明的是,上面的代碼沒有什麼實際意義,僅向咱們展現模擬鍵盤各類按鍵與組合鍵的使用。

在使用鍵盤按鍵方法前須要先導入keys類。

from selenium.webdriver.common.keys import Keys

如下是經常使用的鍵盤操做:

send_keys(Keys.BACK_SPACE)    刪除鍵

send_keys(Keys.SPACE)    空格鍵

send_keys(Keys.TAB)    製表鍵

send_keys(Keys.ESCAPE)  回退鍵

send_keys(Keys.ENTER)  回車鍵

send_keys(Keys.CONTROL,'a')  全選

send_keys(Keys.CONTROL,'c')  複製

send_keys(Keys.CONTROL,'x')   剪切

send_keys(Keys.CONTROL,'v')   粘貼

send_keys(Keys.F1)  鍵盤F1

......

send_keys(Keys.F12)  鍵盤F12

相關文章
相關標籤/搜索