当前位置:网站首页>Selenium element information

Selenium element information

2022-07-05 20:35:00 Live up to your youth

summary

When passed selenium After locating a specific element , You can get many details of this element , For example, the css style 、 Tag name 、 Text and get the child elements of this element, etc .

function

Whether the element is displayed

element.is_displayed()

This method is used to check whether the connected element is correctly displayed on the web page . Returns a Boolean value , If the connected element is displayed in the current browser context , Then for True; Otherwise return to False.

>>> from selenium import webdriver
>>> from selenium.common import By

>>> driver = webdriver.Chrome()
>>> driver.get("https://www.example.com")
>>> element = driver.find_element(By.TAG_NAME, "p")
#  Judge whether the element displays 
>>> element.is_displayed()
True

Whether the element is selected

element.is_selected()

This method determines whether the referenced element has been selected . This method is widely used in check boxes 、 Radio button 、 Input element and option element . Returns a Boolean value , If in the current browsing context Have chosen Referenced element , Then return to True, Otherwise return to False.

Get element attribute value

element.get_attribute(name)

Used to get the attribute value of the element . such as name attribute 、id attribute 、class attribute 、href Properties and so on .

>>> from selenium import webdriver
>>> from selenium.common import By

>>> driver = webdriver.Chrome()
>>> driver.get("https://www.example.com")
>>> element = driver.find_elements(By.TAG_NAME, "p")[1]
>>> element = element.find_element(By.TAG_NAME, "a")
#  obtain href attribute 
>>> element.get_attribute("href")
'https://www.iana.org/domains/example'

Get the element tag name

element.tag_name

This method is used to get The tag name of the referenced element with focus .

>>> from selenium import webdriver
>>> from selenium.common import By

>>> driver = webdriver.Chrome()
>>> driver.get("https://www.example.com")
>>> element = driver.find_element(By.TAG_NAME, "p")
#  Get the tag name 
>>> element.tag_name
'p'

Get element text

element.text

Get the rendered text of a specific element .

>>> from selenium import webdriver
>>> from selenium.common import By

>>> driver = webdriver.Chrome()
>>> driver.get("https://www.example.com")
>>> element = driver.find_element(By.TAG_NAME, "p")
#  Get element text 
>>> element.text
'This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.'

Get elements css style

element.value_of_css_property(property_name)

Gets the value of the specific calculation style attribute of the element in the current browsing context .

>>> from selenium import webdriver
>>> from selenium.common import By

>>> driver = webdriver.Chrome()
>>> driver.get("https://www.example.com")
>>> element = driver.find_element(By.TAG_NAME, "p")
#  Get the text color of the element 
>>> element.value_of_css_property("color")
'rgba(0, 0, 0, 1)'

Get element rectangle

element.rect

Used to obtain the dimensions and coordinates of the reference element . The extracted data body contains X Axis position 、 In the upper left corner of the element y Axis position 、 Height of element 、 Element width .

>>> from selenium import webdriver
>>> from selenium.common import By

>>> driver = webdriver.Chrome()
>>> driver.get("https://www.example.com")
>>> element = driver.find_element(By.TAG_NAME, "p")
#  Get the rectangular coordinates of the element 
>>> element.rect
{'height': 60, 'width': 418, 'x': 32, 'y': 115.67500305175781}

Get element child elements

element.find_element()

Used to find matching child elements in the context of parent elements (WebElement).

>>> from selenium import webdriver
>>> from selenium.common import By

>>> driver = webdriver.Chrome()
>>> driver.get("https://www.example.com")
>>> element = driver.find_elements(By.TAG_NAME, "p")[1]
#  Get the child elements of the element 
>>> element = element.find_element(By.TAG_NAME, "a")
>>> element.text
'More information...'
原网站

版权声明
本文为[Live up to your youth]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051948075315.html

随机推荐