標籤:首頁 display att logs 屬性 env [1] driver image
通過WebElement介面擷取值
size 擷取元素的尺寸
text 擷取元素的文本
get_attribute(name) 擷取屬性值
location 擷取元素座標,先找到要擷取的元素,再調用該方法
page_source 返回頁面源碼
driver.title 返回頁面標題
current_url 擷取當前頁面的URL
is_displayde() 判斷該元素是否可見
is_enabled() 判斷元素是否被使用
is_selected() 判斷元素是否被選中
tag_name 返回元素的tagName
例子:百度首頁的 新聞按鈕
#! /usr/bin/env python#coding=utf-8from selenium import webdriverimport timeurl = "https://www.baidu.com/"driver = webdriver.Firefox()driver.get(url)time.sleep(3)#size擷取元素的尺寸size = driver.find_element_by_id("kw").sizeprint("搜尋方塊的尺寸:",size)#搜尋方塊的尺寸: {‘height‘: 22, ‘width‘: 500}time.sleep(3)#text擷取元素的文本news = driver.find_element_by_name("tj_trnews").textprint("新聞按鈕的文本:",news)#新聞按鈕的文本: 新聞time.sleep(3)#get_attribute(name)擷取屬性值href = driver.find_element_by_xpath(".//*[@id=‘u1‘]/a[1]").get_attribute("href")name = driver.find_element_by_xpath(".//*[@id=‘u1‘]/a[1]").get_attribute("name")print("新聞按鈕的連結值:",href)#新聞按鈕的連結值: http://news.baidu.com/print("新聞按鈕的名字值:",name)#新聞按鈕的名字值: tj_trnewstime.sleep(3)#location擷取元素座標,先找到要擷取的元素,再調用該方法location = driver.find_element_by_xpath(".//*[@id=‘u1‘]/a[1]").locationprint("新聞按鈕的座標值:",location)#新聞按鈕的座標值: {‘x‘: 574, ‘y‘: 19}print("當前頁面的URL:",driver.current_url)#當前頁面的URL: https://www.baidu.com/print("當前頁面的標題:",driver.title)#當前頁面的標題: 百度一下,你就知道result1 = driver.find_element_by_xpath(".//*[@id=‘u1‘]/a[1]").is_displayed()result2 = driver.find_element_by_name("tj_trnews").is_displayed()print("新聞按鈕是否可見1:",result1)print("新聞按鈕是否可見2:",result2)#新聞按鈕是否可見1: True#新聞按鈕是否可見2: Truedriver.quit()
結果:
新聞按鈕的名字值: tj_trnews
新聞按鈕的座標值: {‘x‘: 574, ‘y‘: 19}
當前頁面的URL: https://www.baidu.com/
當前頁面的標題: 百度一下,你就知道
新聞按鈕是否可見1: True
新聞按鈕是否可見2: True
WebElement介面擷取值