Getting values through the Webelement interface
Size gets the dimensions of the element
Text gets the literal of the element
Get_attribute (name) Gets the property value
Location gets the element coordinates, first finds the element to get, and then calls the method
Page_source back to page source code
Driver.title return page title
Current_url gets the URL of the current page
Is_displayde () Determines whether the element is visible
Is_enabled () Determine if the element is being used
Is_selected () Determines whether the element is selected
Tag_name returns the tagname of the element
Example: Baidu homepage of the news button
#! /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 of Search box:", size #搜索框的尺寸: {' height ': $, ' width ': 500}time.sleep (3) #text获取元素的文本news = Driver.find_element_by_name ("Tj_trnews"). Textprint ("Press button text:", News) #新闻按钮的文本: News Time.sleep (3) #get_attribute (name) Gets the property value 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 ("Link value for news button:", href) #新闻按钮的链接值: http://news.baidu.com/print ("News button's name value:", name) #新闻按钮的名字值: tj_ Trnewstime.sleep (3) #location获取元素坐标, first find the element to get, and then call the method location = Driver.find_element_by_xpath (".//*[@id = ' U1 ']/a[1]" ). Locationprint ("press button coordinate value:", location) #新闻按钮的坐标值: {' x ': 574, ' Y ': 19}print ("URL of the current page:", Driver.current_url) #当前页面的URL: Https://www.baidu.com/print ("Title of the current page:", Driver.title) #当前页面的标题: Baidu a bit, you know RESULT1 = Driver.find_eleMent_by_xpath (".//*[@id = ' U1 ']/a[1]"). is_displayed () result2 = Driver.find_element_by_name ("Tj_trnews"). Is_ Displayed () Print ("Press button is visible 1:", RESULT1) Print ("Press button is visible 2:", RESULT2) #新闻按钮是否可见1: true# Press button is visible 2:truedriver.quit ()
Results:
News button's name value: Tj_trnews
The coordinate value of the news button: {' x ': 574, ' y ': 19}
url:https://www.baidu.com/of the current page
Title of the current page: Baidu a bit, you know
Whether the news button is visible 1:true
Whether the news button is visible 2:true
Webelement interface Get Value