WebElement介面擷取值

來源:互聯網
上載者:User

標籤:首頁   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介面擷取值

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.