打印索引元素的文本属性:
def print_list(coordinate_list):
print('当前项目地块数:', len(coordinate_list))
for i in range(0, len(coordinate_list)):
print(i)
print(coordinate_list[i].text)
定义一个根据元素文本获取元素的函数:
def getElementByText(coordinate_list, myvalue):
for i in range(0, len(coordinate_list)):
if coordinate_list[i].text == myvalue:
return coordinate_list[i]
else:
print("Failure!")
获取元素value值
driver.find_element(By.ID, "kw").send_keys("ROOT")
user_name = driver.find_element(By.ID, "kw")
text_user_name = user_name.get_attribute("value")
print(text_user_name)
看一下By支持的方法
class By:
"""Set of supported locator strategies."""
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
示例
element = driver.find_element(By.TAG_NAME,"html")
# 替换为您要检查的标签
attribute_value = element.get_attribute("data-theme") # 替换为您要获取的属性名称
# 打印属性值
print("html标签的属性值:", attribute_value)
CLASS_NAME、NAME、ID同理,以www.baidu.com为例