webElement常用屬性與方法
定位到元素后,除了對(duì)元素進(jìn)行操作,還可以獲取元素的一些屬性信息。常見的屬性信息:
1、獲取元素的尺寸:ele.size
2、獲取元素的坐標(biāo):ele.location
3、獲取元素的文本內(nèi)容:ele.text text是存在在一對(duì)a標(biāo)簽、p標(biāo)簽或div標(biāo)簽中的文本內(nèi)容,如果是標(biāo)簽中的value值,是不能通過這種方式來獲取到的。
4、獲取元素的屬性值:ele.get_attribute(屬性名) 通過傳入不同的屬性名來獲取對(duì)應(yīng)的屬性值
5、獲取頁面的url:driver.current_url 對(duì)url獲取再進(jìn)行判斷,是一種常用的檢查方式
6、獲取頁面的title:driver.title 對(duì)title獲取再進(jìn)行判斷,也還是一種常用的檢查方式文章來源地址http://www.zghlxwxcb.cn/news/detail-800028.html
from selenium import webdriver
# webElement常用屬性與方法
# 打開chrome瀏覽器
driver = webdriver.Chrome()
# 設(shè)置瀏覽器窗口最大化
driver.maximize_window()
# 打開百度首頁
driver.get('https://www.baidu.com')
'''獲取百度搜索框的尺寸和坐標(biāo)'''
# 定位到百度搜索框
ele_search = driver.find_element_by_id('kw')
# 獲取搜索框尺寸
r_size = ele_search.size
print('搜索框的尺寸:', r_size)
# 獲取百度搜索框的坐標(biāo)
r_location = ele_search.location
print('搜索框的坐標(biāo):', r_location)
'''獲取百度首頁底部的備案信息'''
# 定位到底部元素
ele_bottom = driver.find_element_by_id('bottom_layer')
# 獲取元素文本
bottom_text = ele_bottom.text
print('備案信息:', bottom_text)
'''定位到百度搜索按鈕,并獲取這個(gè)標(biāo)簽的其它屬性'''
# 定位到百度一下按鈕
ele_search_button = driver.find_element_by_id('su')
# 獲取搜索按鈕元素的屬性值
ele_search_button_value = ele_search_button.get_attribute('value')
print('搜索按鈕元素的value屬性值:', ele_search_button_value)
'''打開百度的網(wǎng)址,獲取當(dāng)前頁面url和title,來判斷百度首頁是否打開成功'''
# 獲取當(dāng)前頁面的url
url = driver.current_url
print('當(dāng)前頁面的url:', url)
# 獲取當(dāng)前頁面的title
title = driver.title
print('當(dāng)前頁面的title:', title)
if url == 'https://www.baidu.com/' and title == '百度一下,你就知道':
print('打開百度首頁成功')
else:
print('打開百度首頁失敗')
# 關(guān)閉瀏覽器
driver.quit()
文章來源:http://www.zghlxwxcb.cn/news/detail-800028.html
到了這里,關(guān)于selenium之元素常用屬性的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!