Selenium學習之元素屬性值、坐標位置、大小、偏移點擊


在自動化測試過程中,我們可能需要獲取元素的屬性值以及坐標位置、大小,那么怎么獲取這些信息呢?在這一篇我做了下簡單記錄。

(一)get_attribute 獲取元素的給定屬性或屬性

       首先,先來說一下,如何獲取元素的給定屬性或屬性。在Selenium中提供了一個get_attribute()的方法,通過在方法中設置不同的參數內容可以獲取到相應的信息,其中給定屬性提供了下面三種:

(1)獲取元素的文本內容:get_attribute(‘textContent’)

(2)獲取元素的內部HTML:get_attribute('innerHTML')

(3)獲取元素的外部HTML:get_attribute('outerHTML')

       除了給定屬性外,非給定屬性的話,則根據不同元素而定,比如get_attribute('id')可以拿到元素的id,get_attribute('class')可以拿到元素的class等等。這里拿百度首頁的“新聞”元素的父元素來舉例,這是我將要測試的父元素的部分html截圖,顯示如下:

具體代碼操作:獲取百度首頁的“新聞”元素的父元素中的文本內容、內部HTML、外部HTML、新聞元素的href屬性值。

測試代碼如下:

from selenium import webdriver
from time import sleep

class TestCase(object):

    def __init__(self):
        self.driver = webdriver.Chrome()
        self.url = 'http://www.baidu.com'
        self.driver.maximize_window()
        self.driver.get(self.url)

    def test_get_attribute(self):
        dad = self.driver.find_element_by_id('s-top-left')
        news = self.driver.find_element_by_css_selector('#s-top-left .a')
        print('innerHTML =', dad.get_attribute('innerHTML'))
        print('outerHTML =', dad.get_attribute('outerHTML'))
        print('text =', news.get_attribute('textContent'))
        print('news href=', news.get_attribute('href'))

if __name__ == '__main__':
    test = TestCase()
    test.test_get_attribute()

    控制台顯示結果如下:

       可以看到的是get_atrribute('innerHTML')返回的結果不會包括父元素的HTML代碼,而get_atrribute('outerHTML')返回的結果包括,這個通過紅框顯示新聞元素是否有被其他元素包含可以看出。另外,通過get_atrribute('textContent')拿到了新聞元素的文本內容——“新聞”,同時根據href這個屬性名稱拿到了該屬性的值。

(二)獲取元素的坐標位置和大小

        在上一篇的ActionChains模塊的move_to_element_by_offset()方法中其實我已經有運用到了,附上代碼和控制台結果:

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

class TestCase(object):

    def __init__(self):
        self.driver = webdriver.Chrome()
        self.url = 'http://www.baidu.com'
        self.driver.maximize_window()
        self.driver.get(self.url)

    def test_move_to_element_with_offset(self):
        kw = self.driver.find_element_by_id('kw')
        kw_x = kw.location.get('x')#百度搜索框的x坐標
        kw_y = kw.location.get('y')#百度搜索框的y坐標
        print(kw.location, ',kw_x = ', kw_x, ',kw_y = ', kw_y)
        more = self.driver.find_element_by_css_selector('#s-top-left > .s-top-more-btn > a') #“更多”元素
        more_x = more.location.get('x') #“更多”元素的x坐標
        more_y = more.location.get('y') #“更多”元素的y坐標
        print(more.location, ',more_x = ', more_x, ',more_y = ', more_y)
        more_w = more.size.get('width')  #“更多”元素的寬
        more_h = more.size.get('height') #“更多”元素的高
        print(more.size, ',more_w = ', more_w, ',more_h = ', more_h)
        xoffset = kw_x - more_x - more_w / 2
        yoffset = kw_y - more_y - more_h / 2
        ActionChains(self.driver).move_to_element_with_offset(more, xoffset, yoffset).perform()
        sleep(3)
        more_hide_window = self.driver.find_element_by_id('s-top-more')
        print(more_hide_window)

if __name__ == '__main__':
    test = TestCase()
    test.test_move_to_element_with_offset()

控制台顯示結果如下:

        從上面的代碼中,我們可以看到使用了location()方法分別拿到百度搜索框和“更多”元素的坐標位置,元素的坐標返回的是一個json字符串,例如:{'x':633,'y':222} 。通過get('x')和get('y')分別拿到x坐標和y坐標(這個坐標是相對於瀏覽器左上角的偏移量)。要注意的是:獲取元素坐標位置,一定記得先把瀏覽器窗口最大化。

        使用size()方法拿到“更多”元素的大小,元素的大小返回的也是一個json字符串:{'height':15,'width':26},然后通過get('width')和get('height')方法分別拿到元素的寬和高。

其實除了location()和size()方法,在Python中還提供了一個“element.rect”的方式去獲取元素坐標位置和尺寸,話不多說直接上代碼:

def test_get_rect(self):
    more = self.driver.find_element_by_css_selector('#s-top-left > .s-top-more-btn > a') #“更多”元素
    rect = more.rect
    print(rect)
    print('x = ', rect['x'], ',y = ', rect['y'], ',width = ', rect['width'], ',height = ', rect['height'])

控制台顯示結果如下:

 

原文地址:https://blog.csdn.net/shan286/article/details/107682434

 

---------------------------------------------------------------------------------

關注微信公眾號即可在手機上查閱,並可接收更多測試分享~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM