selenium-- page elements related operations

Gets the label elements and element size

from Selenium Import the webdriver
 Import the unittest 


class Test_BasicInfo (of unittest.TestCase):
     DEF test_getBasicInfo (Self): 
        URL = ' http://www.baidu.com ' 
        self.driver = webdriver.Chrome () 
        self.driver.get (URL) 
        the newElement = self.driver.find_element_by_link_text ( ' news ' ) 
        
        # print find basic information elements 
        Print ( ' element tag name: ' , newElement.tag_name)
         Print ( ' element size:', newElement.size)


test1 = Test_BasicInfo()
test1.test_getBasicInfo()

result:

Element tag name: A 
size of elements: { ' height ' : 24, ' width ' :} 26 is

Get the text content of the element

from selenium import webdriver
import unittest
import time


class Test_ElementText(unittest.TestCase):
    def test_getWebElementText(self):
        url = 'http://www.baidu.com'
        self.driver = webdriver.Chrome()
        self.driver.get(url)
        time.sleep(3)

        # 获取元素的文本内容
        a_text = self.driver.find_element_by_xpath('//*[@class="mnav"][1]').text
        print(a_text)


test1 = Test_ElementText()
test1.test_getWebElementText()

result

news

Get attributes of page elements

from Selenium Import the webdriver
 Import the unittest 


class Test_ElementAttribute (of unittest.TestCase):
     DEF test_getWebElementAttribute (Self): 
        URL = ' http://www.sogou.com ' 
        self.driver = webdriver.Chrome () 
        self.driver.get (URL) 
        Query1 = self.driver.find_element_by_id ( ' Query ' ) 

        # Get the search input box name attribute 
        Print (query1.get_attribute ( ' name ' )) 
        query1.send_keys ( ' test development' ) 

        # Get the value of the search box value, both the text input box 
        Print (query1.get_attribute ( ' value ' )) 


test1 = Test_ElementAttribute () 
test1.test_getWebElementAttribute ()

result:

query 
test development

Get css property values ​​of the page

from Selenium Import the webdriver
 Import the unittest 


class Test_ElementCssValue (of unittest.TestCase):
     DEF test_getElementCssValue (Self): 
        URL = ' http://www.baidu.com ' 
        self.driver = webdriver.Chrome () 
        self.driver.get (URL) 
        Case = self.driver.find_element_by_id ( ' kW ' ) 

        # use value_of_css_property () method to get the value of an element attribute css 
        Print ( ' height: ' , case.value_of_css_property ( ' height '))
        print('宽度:', case.value_of_css_property('width'))


test1 = Test_ElementCssValue()
test1.test_getElementCssValue()

result:

Height: 22px 
width: 500px

 

Guess you like

Origin www.cnblogs.com/zouzou-busy/p/11219824.html