WebElement接口獲取值

經過WebElement接口獲取值python

  size  獲取元素的尺寸web

  text  獲取元素的文本ui

  get_attribute(name)  獲取屬性值url

  location  獲取元素座標,先找到要獲取的元素,再調用該方法blog

  page_source  返回頁面源碼接口

  driver.title  返回頁面標題utf-8

  current_url  獲取當前頁面的URLelement

  is_displayde()  判斷該元素是否可見get

  is_enabled()  判斷元素是否被使用源碼

  is_selected()  判斷元素是否被選中

  tag_name  返回元素的tagName

例子:百度首頁的 新聞按鈕 

 

#! /usr/bin/env python
#coding=utf-8

from selenium import webdriver
import time

url = "https://www.baidu.com/"
driver = webdriver.Firefox()
driver.get(url)
time.sleep(3)

#size獲取元素的尺寸
size = driver.find_element_by_id("kw").size
print("搜索框的尺寸:",size)
#搜索框的尺寸: {'height': 22, 'width': 500}
time.sleep(3)

#text獲取元素的文本
news = driver.find_element_by_name("tj_trnews").text
print("新聞按鈕的文本:",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_trnews
time.sleep(3)

#location獲取元素座標,先找到要獲取的元素,再調用該方法
location = driver.find_element_by_xpath(".//*[@id='u1']/a[1]").location
print("新聞按鈕的座標值:",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: True

driver.quit()

  結果:

新聞按鈕的名字值: tj_trnews新聞按鈕的座標值: {'x': 574, 'y': 19}當前頁面的URL: https://www.baidu.com/當前頁面的標題: 百度一下,你就知道新聞按鈕是否可見1: True新聞按鈕是否可見2: True

相關文章
相關標籤/搜索